安卓自定义View - 画一个旋转风车 - 新手笔记

先上效果图:

安卓自定义View - 画一个旋转风车 - 新手笔记_第1张图片

MainActivity.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /******全屏*******/
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //setContentView(R.layout.sample_my_view);
        
        final MyView taiJi = new MyView(this);//实例你的自定义view
        setContentView(taiJi);//设置布局

        @SuppressLint("HandlerLeak")
        final Handler handler = new Handler() {
            private float degrees = 0; //定义旋转角度
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                taiJi.setRotate(degrees += 5);//每次旋转+5
                this.sendEmptyMessageDelayed(0, 80);//调整旋转速度
            }
        };
       
    }
}
MyView.java

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Random;

/**
 * TODO: document your custom view class.
 */
public class MyView extends View {

    private String TAG = "MyView";

    private Paint randomPaint;//定义一个随机颜色的画笔
    private Random mRandom;

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.resize);//风车图片

    public MyView(Context context) {
        super(context);
        Log.d(TAG, "MyView: 1");
        initPaints();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG, "MyView: 2");
        initPaints();
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Log.d(TAG, "MyView: 3");
    }

    private void initPaints() {//初始化画笔
        randomPaint = new Paint();
        blackPaint.setAntiAlias(true);
        randomPaint.setTextSize(100);
        randomPaint.setStyle(Paint.Style.FILL);
        randomPaint.setTextAlign(Paint.Align.CENTER);
        mRandom = new Random();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = canvas.getWidth();   //画布宽度
        int height = canvas.getHeight(); //画布高度
        int view_width = bitmap.getWidth();//风车图片宽度
        int view_height = bitmap.getHeight();//风车图片高度
        Log.d(TAG, "onDraw: "+width +" * "+ height+"----"+view_width +" * "+ view_height);
        canvas.translate(width/2, height/2);//定义一个点,画布中心点
        canvas.drawColor(Color.WHITE);//背景白色

        randomPaint.setColor(Color.rgb(getRandomNumber(0,255),
                getRandomNumber(0,255), getRandomNumber(0,255)));//每次颜色都随机
        canvas.drawRect(-20, 10, 20, 500, randomPaint);//画一支不停变色的风车杆

        canvas.rotate(degrees);//设置旋转

        Paint paint_2 = new Paint();//随便设置一个画笔
        paint_2.setAntiAlias(true);//抗锯齿
        canvas.drawBitmap(bitmap,-view_width/2,-view_height/2,paint_2);//把风车图片画上去

    }

    private float degrees = 0;                  //旋转角度
    public void setRotate(float degrees) {
        this.degrees = degrees;
        invalidate();                        //重绘界面
    }

    public static int getRandomNumber(int Min,int max){//随机一个min Min){
            Random random = new Random();
            return random.nextInt(max - Min) + Min;
        }
        return 0;
    }

}

布局:

sample_my_view.xml:



    
    

1.风车图片:

安卓自定义View - 画一个旋转风车 - 新手笔记_第2张图片

2.git部分代码地址:https://github.com/lingchen1854/-view.git

 

你可能感兴趣的:(自定义view)