亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

ImageView自動來回循環移動,用到Animation

標簽:
Java Android

我这个移动是只在手机屏幕的最上方移动,需要更改的可以更改。

随着学习,经验是增加了,但是还是很缺少经验,我分享这个代码只是更大家分享小小的成果,顺便永久性保存这个代码,以后用到的时候可以拿来就用,需要改正也是很方便的,毕竟自己动脑子写的,每一步的功能都理解的很清楚。

package com.example.yidong3;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView im;
private int ScreenWidth;
private int ScreenHeight;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

                    /*获取屏幕宽高*/
    DisplayMetrics  dm = new DisplayMetrics();      

    getWindowManager().getDefaultDisplay().getMetrics(dm);      

    ScreenWidth = dm.widthPixels;                

    ScreenHeight = dm.heightPixels;  

    im = (ImageView) findViewById(R.id.imageView1);

            Animation();

}

private void Animation() {
    // TODO Auto-generated method stub

    // TODO Auto-generated method stub
    AnimationSet animationSet = new AnimationSet(true);

    TranslateAnimation translateAnimation = new TranslateAnimation(
            // X轴的开始位置
            android.view.animation.Animation.RELATIVE_TO_SELF, -1f,
            // X轴的结束位置
            android.view.animation.Animation.RELATIVE_TO_SELF, ScreenWidth/80,
            // Y轴的开始位置
            android.view.animation.Animation.RELATIVE_TO_SELF, 0f,
            // Y轴的结束位置
            android.view.animation.Animation.RELATIVE_TO_SELF, 0f);
    translateAnimation.setDuration(1000);
    translateAnimation.setRepeatCount(20);  //  设置动画重复次数

    translateAnimation.setRepeatMode(android.view.animation.Animation.REVERSE);
    //translateAnimation.setRepeatMode(Animation.RESTART);    //重新从头执行  
    //translateAnimation.setRepeatMode(Animation.REVERSE);  //反方向执行  

    animationSet.addAnimation(translateAnimation);
    im.setAnimation(animationSet);

}

}

點擊查看更多內容
3人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消