android animation应用——图片绕固定点旋转

一、功能:实现将图片绕固定点旋转,圈数随机,onTouch后旋转。

二、程序框架:

组成 功能
主Activity:MyActivity 1.实现animation
2.实现onTouch
View       :MyView 1.将突破绘制到MyView上
三、程序源代码:

MyVIew.java

package com.androids.kavinapps.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

/**
 * Created by Administrator on 14-11-29.
 */
public class MyView extends View{
    //define roate animatioin
    public Animation mAnimationRoate;
    //define bitmap object
    Bitmap mBitmap = null;
    public MyView(Context context) {
        super(context);
        //load resource
        mBitmap = ((BitmapDrawable)getResources().getDrawable(com.androids.kavinapps.myapplication.R.drawable.choujiang1)).getBitmap();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint mPaint = null;
        //draw pic
        canvas.drawBitmap(mBitmap,0,40,null);
    }

}


MyActivity.java

package com.androids.kavinapps.myapplication;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Message;
import android.os.Handler;//Handler
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;



public class MyActivity extends Activity {

    AnimationDrawable mAnimation1 = null;
    int mRandom = 1;//随机数
    MyView myView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myView = new MyView(this);
        setContentView(myView);

        mRandom = (int) (Math.random()*100);
        if(mRandom%5==0){
            mRandom = 5;
        }else {
            mRandom = mRandom%5;
        }

        myView.mAnimationRoate = new RotateAnimation(0.0f, +(1800.0f +72*mRandom), Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
        //set the time of anim
        myView.mAnimationRoate.setDuration(3000);
        myView.mAnimationRoate.setFillAfter(true);//动画完成后不恢复原状
        myView.startAnimation(myView.mAnimationRoate);
    }//onCreate
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mRandom = (int) (Math.random()*100);
                if(mRandom%5==0){
                    mRandom = 5;
                }else {
                    mRandom = mRandom%5;
                }

                myView.mAnimationRoate = new RotateAnimation(0.0f, +(1800.0f +72*mRandom), Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
                myView.mAnimationRoate.setDuration(3000);
                myView.mAnimationRoate.setFillAfter(true);//动画完成后不恢复原状
                myView.startAnimation(myView.mAnimationRoate);
                return true;
        }
        return true;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

四、部分代码分析

1.如何产生随机数

mRandom = (int) (Math.random()*100);
2.如何使动画完成后,不恢复原装填

myView.mAnimationRoate.setFillAfter(true);//动画完成后不恢复原状

3.如何将drawable下的图片文件变为Bitmap

mBitmap = ((BitmapDrawable)getResources().getDrawable(com.androids.kavinapps.myapplication.R.drawable.choujiang1)).getBitmap();

2014年12月9日14:41:34

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。