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
注意:平移后会回到原位,所以要添加最后一句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);
}
效果:
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);
}
核心代码:
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中的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));
}
正常
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;
}