自定义view实现画个闪烁的心

package com.loaderman.lovecircledemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LoveView view = new LoveView(this);
        setContentView(view);
    }
}

 

package com.loaderman.lovecircledemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class LoveView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
    boolean       mbloop         = false;
    SurfaceHolder mSurfaceHolder = null;
    private Canvas canvas;
    int miCount = 0;
    int y       = 50;
    /**
     * @param context
     */
    public LoveView(Context context) {
        super(context);
        mSurfaceHolder = this.getHolder();
        mSurfaceHolder.addCallback(this);
        this.setFocusable(true);
        this.setKeepScreenOn(true);
        mbloop = true;
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new Thread(this).start();
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

        mbloop = false;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (mbloop) {
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                // TODO: handle exception
            }
            synchronized (mSurfaceHolder) {
                Draw();
            }
        }
    }

    private void Draw() {
        // TODO Auto-generated method stub
        canvas = mSurfaceHolder.lockCanvas();
        try {
            if (mSurfaceHolder == null || canvas == null) {
                return;
            }
            if (miCount < 100) {
                miCount++;
            } else {
                miCount = 0;
            }
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.BLACK);
            canvas.drawRect(0, 0, 320, 480, paint);
            switch (miCount % 6) {
                case 0:
                    paint.setColor(Color.BLUE);
                    break;
                case 1:
                    paint.setColor(Color.GREEN);
                    break;
                case 2:
                    paint.setColor(Color.RED);
                    break;
                case 3:
                    paint.setColor(Color.YELLOW);
                    break;
                case 4:
                    paint.setColor(Color.argb(255, 255, 181, 216));
                    break;
                case 5:
                    paint.setColor(Color.argb(255, 0, 255, 255));
                    break;
                default:
                    paint.setColor(Color.WHITE);
                    break;
            }
            int i, j;
            double x, y, r;
            for (i = 0; i <= 90; i++) {
                for (j = 0; j <= 90; j++) {
                    r = Math.PI / 45 * i * (1 - Math.sin(Math.PI / 45 * j)) * 20;
                    x = r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i) + 320 / 2;
                    y = -r * Math.sin(Math.PI / 45 * j) + 400 / 4;
                    canvas.drawPoint((float) x, (float) y, paint);
                }
            }
            paint.setTextSize(32);
            paint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
            RectF rect = new RectF(60, 400, 260, 405);
            canvas.drawRoundRect(rect, (float) 1.0, (float) 1.0, paint);
            canvas.drawText("Loving You", 75, 400, paint);
            mSurfaceHolder.unlockCanvasAndPost(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 效果图:

  1. 画图的时候你可以继承View,也可以继承SurfaceView,这两者的区别在于:surfaceView是在一个新起的单独线程中可以重新绘制画面而View必须在UI的主线程中更新画面。SurfaceView可以控制表面的格式,比如大小,显示在屏幕中的位置,最关键是的提供了SurfaceHolder类,使用getHolder方法获取,还有涉及的surfaceCreated(SurfaceHolder holder),surfaceDestroyed(SurfaceHolder holder),surfaceChanged(SurfaceHolder holder, int format, int width, int height)方法,而在SurfaceHolder.Callback 接口回调中可以通过重写来改变这些方法
  2. 程序其实很简单, 既然生命了Runnable接口,就有相对应的Run方法,在surfaceCreate()的时候开启线程,线程每隔500ms就刷新一次,这样我们看到的效果就是闪烁的,每500毫秒 画一次图,根据经过的间隔时间来设置画笔的颜色,然后通过循环描点,画出心形,然后设置字体大小,画字和字下面的横线。

本文学习来源:http://www.jianshu.com/p/69819295a0ef


 

posted on 2017-03-17 14:24  LoaderMan  阅读(199)  评论(0编辑  收藏  举报

导航