Android Studio||动态改变xml图片位置+背景/旋转+平移/AnimationSet/java读取drawable图

16 篇文章 0 订阅

step by step.

目录

参考:

平移(TranslatAnimation)

旋转(RotateAnimation)

AnimationSet 

xml:(正常设置即可)

java:

java读取drawable图


参考:

Android studio——动画_androidstudio动画效果_孙权ph的博客-CSDN博客这里写目录标题帧动画FrameAnimation补间动画TweenAnimationTranslatAnimation:平移动画ScaleAnimation:缩放动画alphaAnimation:淡出动画rotateAnimation:旋转动画setAnimation :动画集,可以包含多个动画Animator(属性动画):真正改变了动画的属性常见属性ValueAnimator:值动画PropertyValueHolder:多个动画同时执行帧动画FrameAnimation按字面理解,就是将多个图片连续https://blog.csdn.net/crrr_r/article/details/118959074?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%B9%B3%E7%A7%BB%E6%97%8B%E8%BD%ACandroid%20studio%E5%90%8C%E6%97%B6&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-118959074.142%5Ev77%5Einsert_down38,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&spm=1018.2226.3001.4187

深入理解Android中的Drawable类_@android:drawable在哪个类库里_Cmdmac的博客-CSDN博客Drawable对于Android开发工程师来说非常熟悉,最常用的用法是在drawable目录里放入png或其他格式的图片,然后在代码里就可以用resources访问到如:// 访问test图片资源getResources().getDrawable(R.drwable.test);这里不是要讲Drawable资源怎么使用,而是来看一下这个类实现的一些原理以及它相关的一些子类的实现原理。...https://blog.csdn.net/CmdMac/article/details/85250912?ops_request_misc=&request_id=&biz_id=102&utm_term=android%20studio%20java%E5%BC%95%E7%94%A8drwable&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-4-85250912.142%5Ev77%5Einsert_down38,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&spm=1018.2226.3001.4187

平移(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);
    }

效果:

 

 

 

 

旋转(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);
    }

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

java读取drawable图

深入理解Android中的Drawable类_@android:drawable在哪个类库里_Cmdmac的博客-CSDN博客Drawable对于Android开发工程师来说非常熟悉,最常用的用法是在drawable目录里放入png或其他格式的图片,然后在代码里就可以用resources访问到如:// 访问test图片资源getResources().getDrawable(R.drwable.test);这里不是要讲Drawable资源怎么使用,而是来看一下这个类实现的一些原理以及它相关的一些子类的实现原理。...https://blog.csdn.net/CmdMac/article/details/85250912?ops_request_misc=&request_id=&biz_id=102&utm_term=android%20studio%20java%E5%BC%95%E7%94%A8drwable&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-4-85250912.142%5Ev77%5Einsert_down38,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&spm=1018.2226.3001.4187

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;
    }

效果:

 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,我们可以使用 `TranslateAnimation` 类来实现 View 的平移动画效果。以下是从左向右平移的示例代码: ```java TranslateAnimation animation = new TranslateAnimation(0, 200, 0, 0); animation.setDuration(1000); // 动画持续时间,单位为毫秒 animation.setFillAfter(true); // 动画结束后保持状态 view.startAnimation(animation); // 开始动画 ``` 上述代码中,`TranslateAnimation` 的两个参数分别表示 View 在 X 轴和 Y 轴上的起点和终点位置。这里我们将 X 轴的起点设置为 0,终点设置为 200,表示 View 从左向右平移 200 个像素。 如果想要实现循环播放的效果,可以使用 `AnimationSet` 类来组合多个动画,示例代码如下: ```java TranslateAnimation animation1 = new TranslateAnimation(0, 200, 0, 0); animation1.setDuration(1000); animation1.setInterpolator(new LinearInterpolator()); // 线性插值器,使动画匀速播放 TranslateAnimation animation2 = new TranslateAnimation(200, 0, 0, 0); animation2.setDuration(1000); animation2.setStartOffset(1000); // 设置动画延迟开始的时间,单位为毫秒 animation2.setInterpolator(new LinearInterpolator()); AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(animation1); animationSet.addAnimation(animation2); animationSet.setRepeatCount(Animation.INFINITE); // 设置动画重复播放的次数 view.startAnimation(animationSet); ``` 上述代码中,我们创建了两个 `TranslateAnimation`,分别表示 View 从左向右平移和从右向左平移。然后使用 `AnimationSet` 将两个动画组合在一起,并设置为循环播放,效果如下: ![translate_animation.gif](https://img-blog.csdn.net/20180420202704371?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFuZ3VhZ2VfYmFpZHU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/75|watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFuZ3VhZ2VfYmFpZHU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/75) 如果想要实现手势控制平移的效果,可以使用 `ViewPropertyAnimator` 类来实现,示例代码如下: ```java view.animate().translationX(200); // 平移 View 到 X 轴 200 像素的位置 ``` 上述代码中,我们使用 `animate()` 方法获取 `ViewPropertyAnimator` 对象,然后调用 `translationX()` 方法实现 View 的平移动画。注意,这种方式只能实现单次平移动画,不能循环播放。 除了平移动画,Android 中还有旋转和缩放动画,可以使用 `RotateAnimation` 和 `ScaleAnimation` 类来实现。具体用法与 `TranslateAnimation` 类似。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值