平移(TranslatAnimation)

注意:平移后会回到原位,所以要添加最后一句setY。但是这里的值和平移的Y又不同。

private void win(ImageView child){
        //向上爬动态
        Animation translationAnimation=new TranslateAnimation(0,0,0,-12);
        translationAnimation.setDuration(500);//设置持续时间
        child.startAnimation(translationAnimation);
        child.setY(child.getY()-90);
    }

效果:

 

android 图片 移动顺序 androidstudio图片位置怎么移动_android studio

 

android 图片 移动顺序 androidstudio图片位置怎么移动_xml_02

 

android 图片 移动顺序 androidstudio图片位置怎么移动_ide_03

 

android 图片 移动顺序 androidstudio图片位置怎么移动_android 图片 移动顺序_04

旋转(RotateAnimation)

public void run2(ImageView cat){                         
       //旋转
        Animation rotateAn =new RotateAnimation(-5, 5, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAn.setDuration(1000);//设置持续时间
        rotateAn.setRepeatCount(12);
        cat.startAnimation(rotateAn);
    }

AnimationSet 

xml:(正常设置即可)

<androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/cat"
        android:layout_width="139dp"
        android:layout_height="111dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="55dp"
        android:layout_marginTop="100dp"
        android:pivotX="80%"
        android:background="@drawable/pic_cat_run"
        android:scaleX="-1" />

java:

核心代码:

Animation rotateAn =new RotateAnimation(-5, 5, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //旋转-5°~+5°,0.5f代表50%,中心位置为原点AnimationSet st = new AnimationSet(true);st.addAnimation(rotateAn); Animation translationAnimation=new TranslateAnimation(-200,2500,0,0); //平移st.addAnimation(translationAnimation); cat.startAnimation(st);

public void run2(ImageView cat){                         //小猫旋转+平移
       //旋转
        Animation rotateAn =new RotateAnimation(-5, 5, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAn.setDuration(1000);//设置持续时间
        rotateAn.setRepeatCount(12);
        //集
        AnimationSet st = new AnimationSet(true);
        st.addAnimation(rotateAn);
        //平移
        Animation translationAnimation=new TranslateAnimation(-200,2500,0,0);
        translationAnimation.setDuration(12000);//设置持续时间
        st.addAnimation(translationAnimation);
        cat.startAnimation(st);
    }

效果:(一边旋转一边移动)

android 图片 移动顺序 androidstudio图片位置怎么移动_android 图片 移动顺序_05

android 图片 移动顺序 androidstudio图片位置怎么移动_android 图片 移动顺序_06

android 图片 移动顺序 androidstudio图片位置怎么移动_xml_07

android 图片 移动顺序 androidstudio图片位置怎么移动_android_08

java读取drawable图

深入理解Android中的Drawable类_@android:drawable在哪个类库里_Cmdmac的博客-CSDN博客Drawable对于Android开发工程师来说非常熟悉,最常用的用法是在drawable目录里放入png或其他格式的图片,然后在代码里就可以用resources访问到如:// 访问test图片资源getResources().getDrawable(R.drwable.test);这里不是要讲Drawable资源怎么使用,而是来看一下这个类实现的一些原理以及它相关的一些子类的实现原理。...

an.setBackground(getResources().getDrawable(R.drawable.pic_icon_tian));

public void run() {
                tip.setVisibility(View.INVISIBLE);
                an.setTextColor(0xff2c2c2c);
                an.setBackground(getResources().getDrawable(R.drawable.pic_icon_tian));
            }

 

轮流踢球效果:

xml:

正常

java:

CountDownTimer timer = new CountDownTimer(8000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            time="本次传球还有 " + (millisUntilFinished /  1000 ) +  " 秒";
            TextView text = (TextView) findViewById(R.id.time);
            text.setText(time);
        }
        @Override
        public void onFinish() {
            Intent intent = new Intent(TestActivity_pa_co.this,TestActivity_pa_co_end.class);
            intent.putExtra("grade","本次成绩为:"+grade+"分!");
            timer.cancel();
            startActivity(intent);
        }
    };
    int grade=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_pa_co);
        ImageView yeman = (ImageView) findViewById(R.id.yeman);
        ImageView redman = (ImageView) findViewById(R.id.redman);
        ImageView ball = (ImageView) findViewById(R.id.ball);
        timer.start();

        //正确赋值
        TextView right1 = (TextView) findViewById(R.id.choice_1f);
        TextView right2 = (TextView) findViewById(R.id.choice_2a);

        right1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                answer_rig(yeman,right1,findViewById(R.id.right1),2);
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        kick(ball,2);
                    }
                }, 300);
            }
        });
        right2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                answer_rig(redman,right2,findViewById(R.id.right2),1);
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        kick(ball,1);
                    }
                }, 300);
            }
        });
        //错误赋值
        answer_wro(findViewById(R.id.choice_1a),findViewById(R.id.wro_1a));
        answer_wro(findViewById(R.id.choice_1b),findViewById(R.id.wro_1b));
        answer_wro(findViewById(R.id.choice_1c),findViewById(R.id.wro_1c));
        answer_wro(findViewById(R.id.choice_1d),findViewById(R.id.wro_1d));
        answer_wro(findViewById(R.id.choice_1e),findViewById(R.id.wro_1e));
        answer_wro(findViewById(R.id.choice_2f),findViewById(R.id.wro_2f));
        answer_wro(findViewById(R.id.choice_2b),findViewById(R.id.wro_2b));
        answer_wro(findViewById(R.id.choice_2c),findViewById(R.id.wro_2c));
        answer_wro(findViewById(R.id.choice_2d),findViewById(R.id.wro_2d));
        answer_wro(findViewById(R.id.choice_2e),findViewById(R.id.wro_2e));
        answer_wro(findViewById(R.id.choice_2f),findViewById(R.id.wro_2f));
    }
    private void success(ImageView img,int f){
        Animation ro_img;
        //选出正确答案的动画,f是方式,1是redman,2是yeman
        if(f == 1){
            ro_img = new RotateAnimation(
                    -30,20,Animation.RELATIVE_TO_SELF,
                    0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        }
        else ro_img = new RotateAnimation(
                30,-20,Animation.RELATIVE_TO_SELF,
                0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        ro_img.setDuration(800);
        img.startAnimation(ro_img);
        addGrade();
        timer.cancel();
        timer.start();
    }
    private void kick(ImageView ball, int f){
        //f=1,redman,right->left
        //f=2,yeman,left->right
        AnimationSet kickAnima = new AnimationSet(true);
        Handler handler = new Handler();
        Animation rotaAnima,tranAnima;
        if(f==1){
            //left<-right
            tranAnima = new TranslateAnimation(15,-220,0,-520);
            tranAnima.setDuration(1400);
            rotaAnima = new RotateAnimation(10,-10,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            rotaAnima.setDuration(1000);
            kickAnima.addAnimation(tranAnima);
            kickAnima.addAnimation(rotaAnima);
            ball.startAnimation(kickAnima);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ball.setY(ball.getY()-480);
                    ball.setX(ball.getX()-300);
                }
            }, 1400);
        }
        else {
            //left->right
            tranAnima = new TranslateAnimation(0,280,0,520);
            tranAnima.setDuration(1400);
            rotaAnima = new RotateAnimation(-10,10,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            rotaAnima.setDuration(1000);
            kickAnima.addAnimation(tranAnima);
            kickAnima.addAnimation(rotaAnima);
            ball.startAnimation(kickAnima);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ball.setY(ball.getY()+480);
                    ball.setX(ball.getX()+300);
                }
            }, 1400);
        }


    }
    public void answer_wro(TextView choice,ImageView tip){
        choice.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //错误后提示,结束游戏
                tip.setVisibility(View.VISIBLE);
                Intent intent = new Intent(TestActivity_pa_co.this,TestActivity_pa_co_end.class);
                intent.putExtra("grade","本次成绩为:"+grade+"分!");
                timer.cancel();
                startActivity(intent);
            }
        });
    }
    public void answer_rig(ImageView man,TextView choice,ImageView tip,int f){
        //正确后提示
        tip.setVisibility(View.VISIBLE);
        Recover(tip);
        success(man,f);
    }
    public void Recover(ImageView tip){
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //恢复原样
                tip.setVisibility(View.INVISIBLE);
            }
        }, 1500);
    }
    private void addGrade(){
        grade+=10;
    }

效果:

android 图片 移动顺序 androidstudio图片位置怎么移动_android_09

 

android 图片 移动顺序 androidstudio图片位置怎么移动_xml_10

android 图片 移动顺序 androidstudio图片位置怎么移动_ide_11

 

 

android 图片 移动顺序 androidstudio图片位置怎么移动_ide_12