Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android animation應用——圖片繞固定點旋轉

android animation應用——圖片繞固定點旋轉

編輯:關於Android編程

一、功能:實現將圖片繞固定點旋轉,圈數隨機,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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved