android-自定义视图-旋转的方块

package com.example.learndrawapi;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Handler;import android.util.AttributeSet;import android.view.View;/** * 自定义方块类 * * @author wengcheng * */public class RotatingRect extends View {public RotatingRect(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initProperties();}public RotatingRect(Context context, AttributeSet attrs) {super(context, attrs);initProperties();}public RotatingRect(Context context) {super(context);initProperties();}private void initProperties() {paint = new Paint();paint.setColor(Color.RED);}@Overridepublic void draw(Canvas canvas) {super.draw(canvas);canvas.save();// 保存Canvas的状态canvas.translate(200, 200);canvas.rotate(degrees, 50, 50);canvas.drawRect(0, 0, 100, 100, paint);degrees++;canvas.restore();// 恢复// 使这个view无效,无效就可以重新绘制,绘制完立刻无效会消耗大量资源,简易延迟绘制handler.sendEmptyMessageDelayed(1, 500);}private Handler handler = new Handler() {public void dispatchMessage(android.os.Message msg) {if (1 == msg.what) {System.out.println("--------------");invalidate();}};};private Paint paint;private float degrees = 0;// 角度}
阅读更多
换一批

没有更多推荐了,返回首页