android 图片翻转

1。import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
import android.widget.ImageView;


public class RotateAnimation extends Animation {


private final float mFromDegrees;//开始角度
private final float mToDegrees; //结束角度
private final float mCenterX; //开始的中心点x,y,z
private final float mCenterY;
private final float mDepthZ;
private final boolean mReverse; //是否需要扭曲
private Camera mCamera; //摄像头

/**
* @category Constructor category:类型,部门,种类,类别,类目;[逻,哲]范畴;体重等级
* @param mFromDegrees //start angle of rotation 开始角度
* @param mToDegrees //the end of rotation angle 结束角度
* @param mCenterX //x-axis value x轴值
* @param mCenterY //y-axis value 
* @param mDepthZ //z-axis value
* @param mReverse //whether the bend
*/
public RotateAnimation(float mFromDegrees, float mToDegrees,
float mCenterX, float mCenterY, float mDepthZ, boolean mReverse) {
super();
this.mFromDegrees = mFromDegrees;
this.mToDegrees = mToDegrees;
this.mCenterX = mCenterX;
this.mCenterY = mCenterY;
this.mDepthZ = mDepthZ;
this.mReverse = mReverse;
}

/*
* (non-Javadoc)
* @see android.view.animation.Animation#initialize(int, int, int, int)
* Initialize this animation with the dimensions(规模,尺寸规格) of the object being animated as well as the objects parents. (This is to support animation sizes being specifed relative(亲属,亲戚;相关物) to these dimensions.) 
* Objects that interpret Animations should call this method when the sizes of the object being animated and its parent are known, and before calling getTransformation(long, Transformation).
*/
/**
* @param width Width of the object being animated
* @param height Height of the object being animated
* @param parentWidth Width of the animated(动画片的) object's parent
* @param parentHeight Height of the animated object's parent
*/
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
// TODO Auto-generated method stub
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}

//生成Transformation
/*
* (non-Javadoc)
* @see android.view.animation.Animation#applyTransformation(float, android.view.animation.Transformation)
*/
/**
* @param interpolatedTime The value of the normalized time (0.0 to 1.0) after it has been run through the interpolation function
* @param t The Transformation object to fill in with the current transforms
*/
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
final float fromDegrees = mFromDegrees;
//生成中间角度
float degrees = fromDegrees + ((mToDegrees - fromDegrees)*interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if(mReverse){
camera.translate(0.0f, 0.0f, mDepthZ*interpolatedTime);
}else{
camera.translate(0.0f, 0.0f, mDepthZ*(1.0f-interpolatedTime));
}
camera.rotateY(degrees); //Applies a rotation(旋转) transform(变换) around the Y axis.
//取得变换后的矩阵
camera.getMatrix(matrix); //Computes the matrix corresponding to the current transformation and copies it to the supplied matrix object.
camera.restore(); //Restores the saved state, if any.

matrix.preTranslate(-centerX, -centerY); //Preconcats the matrix with the specified translation. M' = M * T(dx, dy) 
matrix.postTranslate(centerX, centerY);
}





// private void applyRotation(float start, float end, final int viewId)
private void applyRotation(final int viewId,final ViewGroup mContainer,final ImageView imageViewGone,final ImageView imageViewVisible){
final float centerX = mContainer.getWidth() / 2.0f;
        final float centerY = mContainer.getHeight() / 2.0f;
//        RotateAnimation rotation =
//            new RotateAnimation(start, end, centerX, centerY, 200.0f, true);
//        rotation.setDuration(500);
//        rotation.setInterpolator(new AccelerateInterpolator());
//        rotation.setAnimationListener(new AnimationListener() 
        setDuration(500);
        setInterpolator(new AccelerateInterpolator());
        setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {


mContainer.post(new Runnable() {
@Override
public void run() {
// if(viewId == R.id.imageview1){
// imageView1.setVisibility(View.GONE);
// imageView2.setVisibility(View.VISIBLE);
// }else if (viewId == R.id.imageview2) {
// imageView2.setVisibility(View.GONE);
// imageView1.setVisibility(View.VISIBLE);
// }
imageViewGone.setVisibility(View.GONE);
imageViewVisible.setVisibility(View.VISIBLE);
RotateAnimation rotatiomAnimation = new RotateAnimation(-90, 0, centerX, centerY, 200.0f, false);
rotatiomAnimation.setDuration(500);
rotatiomAnimation.setInterpolator(new DecelerateInterpolator());

mContainer.startAnimation(rotatiomAnimation);
}
});

}
@Override
public void onAnimationRepeat(Animation arg0) {
}


@Override
public void onAnimationStart(Animation arg0) {
}
        });
        //mContainer.startAnimation(rotation);
        mContainer.startAnimation(this);
}



}




///

2.package test.IBrave.game;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class EX04_16B extends Activity {
private static ViewGroup[] viewGroup = new ViewGroup[3];
private static ImageView[][] imageViewGroup = new ImageView[3][2];
private static int[] sa = new int[]{
R.drawable.p01,R.drawable.p02,R.drawable.p03
};


private TextView textView;
private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        viewGroup[0] = (ViewGroup)findViewById(R.id.viewGroupLeft);
        viewGroup[0].setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
        viewGroup[1] = (ViewGroup)findViewById(R.id.viewGroupMiddle);
        viewGroup[1].setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
        viewGroup[2] = (ViewGroup)findViewById(R.id.viewGroupRight);
        viewGroup[2].setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
        
        imageViewGroup[0][0] = (ImageView)findViewById(R.id.imageView1);
        imageViewGroup[0][1] = (ImageView)findViewById(R.id.imageViewB1);
        
        imageViewGroup[1][0] = (ImageView)findViewById(R.id.imageView2);
        imageViewGroup[1][1] = (ImageView)findViewById(R.id.imageViewB2);
        
        imageViewGroup[2][0] = (ImageView)findViewById(R.id.imageView3);
        imageViewGroup[2][1] = (ImageView)findViewById(R.id.imageViewB3);
        
        textView = (TextView)findViewById(R.id.text);
        button = (Button)findViewById(R.id.againBtn);
        
        //button.setClickable(false);
        button.setVisibility(View.GONE);
        random();
        //left
        imageViewGroup[0][0].setOnClickListener(listener);       
        //middle
        imageViewGroup[1][0].setOnClickListener(listener);  
        //right
        imageViewGroup[2][0].setOnClickListener(listener);
        
        
        //濂藉儚鍙互鎵撴柇鍏剁炕杞?褰揇uration鏃堕棿璁剧疆闀夸竴鐐瑰氨鍙互鐪嬪嚭鏉?
        button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

button.setClickable(false);
for(int i=0;i<3;i++){
//涓夊紶鐗屼笉鏄悓鏃剁炕杞?
executeRotation(v,0, -90,viewGroup[i], imageViewGroup[i][1], imageViewGroup[i][0]);
//imageViewGroup[i][0].setClickable(true);
imageViewGroup[i][1].setAlpha(255);
random();
}
}
});
        
    }
    /**
     * 娲楃墝
     */
    private void random(){
    for(int i=0;i<3;i++){
    int tmp = sa[i];
    int s = (int)(Math.random()*2);
    sa[i]=sa[s];
    sa[s]=tmp;
    }
    }
    
    /**
     * 
     * @param start 寮€濮嬬殑瑙掑害
     * @param end 缁撴潫鐨勮搴?
     * @param viewGroup
     * @param imageViewGone 璁剧疆涓洪潪鍙鎬х殑鍥剧墖
     * @param imageViewVisible 璁剧疆涓哄彲瑙佹€х殑鍥剧墖
     */
    private void executeRotation(final View v,final float start,final float end,final ViewGroup viewGroup,final ImageView imageViewGone,final ImageView imageViewVisible){
final float centerX = viewGroup.getWidth() / 2.0f;
        final float centerY = viewGroup.getHeight() / 2.0f;
        RotateAnimation rotation =
            new RotateAnimation(start, -end, centerX, centerY, 0f, true);
        //how long this animation should last.The duration cannot be negative//璐熸暟
        rotation.setDuration(500);
        //Sets the acceleration(鍔犻€? curve(k蓹:v鏇茬嚎) for this animation. Defaults to a linear(藞lini蓹) interpolation(绡℃敼锛涙坊鍐欙紝鎻掕ˉ).
        rotation.setInterpolator(new AccelerateInterpolator());
        viewGroup.startAnimation(rotation);
        rotation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {


viewGroup.post(new Runnable() {
@Override
public void run() {
imageViewGone.setVisibility(View.GONE);
imageViewVisible.setVisibility(View.VISIBLE);
RotateAnimation rotatiomAnimation = new RotateAnimation(end, start, centerX, centerY, 0f, false);
rotatiomAnimation.setDuration(500);
rotatiomAnimation.setInterpolator(new DecelerateInterpolator());
viewGroup.startAnimation(rotatiomAnimation);
rotatiomAnimation.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if( v != findViewById(R.id.againBtn) ){
boolean isWin = false;
if( v == findViewById(R.id.imageView1) ){
//璁剧疆鍥剧墖鐨勯€忔槑搴?
imageViewGroup[1][1].setAlpha(160);
imageViewGroup[2][1].setAlpha(160);
//妫€楠屾槸鍚︾寽瀵逛簡
if(sa[0]==R.drawable.p01){
isWin = true;
}
}else if( v == findViewById(R.id.imageView2) ){
imageViewGroup[0][1].setAlpha(160);
imageViewGroup[2][1].setAlpha(160);

if(sa[1]==R.drawable.p01){
isWin = true;
}
}else{
imageViewGroup[0][1].setAlpha(160);
imageViewGroup[1][1].setAlpha(160);

//if( imageViewGroup[2][1] == findViewById(R.drawable.p01) ){
if( sa[2] == R.drawable.p01 ){
isWin = true;
}
}

if(isWin){
textView.setText(R.string.win);
}else{
textView.setText(R.string.lose);
}
//鎸夐挳鍙
button.setVisibility(View.VISIBLE);
button.setClickable(true);
}else{
textView.setText(R.string.guess);
button.setVisibility(View.GONE);
}
}
});
}
});

}
@Override
public void onAnimationRepeat(Animation arg0) {
}


@Override
public void onAnimationStart(Animation arg0) {
if( ( button.getVisibility() == View.GONE ) ){
textView.setText("璋滈鍗冲皢鎻檽鈥︹€?);
}
}
        });
        
}
    
    View.OnClickListener listener = new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0; i < 3;i++){
//random()鍚庢瘡娆¢兘瑕侀噸鏂拌缃浘鐗?
imageViewGroup[i][1].setImageDrawable(getResources().getDrawable(sa[i]));
imageViewGroup[i][1].setClickable(false);//鍙互涓嶉渶瑕?
executeRotation(v,0, 90,viewGroup[i], imageViewGroup[i][0], imageViewGroup[i][1]);
}
}
};

    
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值