午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

Android 繪制一個(gè)Loading動(dòng)畫

 mandrave 2014-09-24
Android 繪制一個(gè)Loading動(dòng)畫__向圖片中緩慢填充顏色,從而形成動(dòng)畫效果

需求:制作一個(gè)加載動(dòng)畫,向一個(gè)不規(guī)則圖片圖形中從從下到上依次填充顏色,形成動(dòng)畫效果。

效果如下:

   

代碼如下:

LoadingAnimatorView.java

復(fù)制代碼
package cn.yw.lib.animation;

import cn.yw.lib.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class LoadingAnimatorView extends SurfaceView implements
        SurfaceHolder.Callback, Runnable {
    private SurfaceHolder holder;
    private Bitmap bitmap;
    private Paint paint1;
    private Paint paint2;
    public boolean flag = true;
    private int y = 100;

    public LoadingAnimatorView(Context context) {
        super(context);
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
        holder = this.getHolder();
        holder.addCallback(this);
        paint1 = new Paint();
        paint1.setColor(Color.RED);
        paint2 = new Paint();
        paint2.setColor(Color.GRAY);
        Bitmap bitmap1 = BitmapFactory.decodeStream(context.getResources()
                .openRawResource(R.drawable.ic_launcher));
        bitmap = bitmap1.extractAlpha();// 獲取一個(gè)透明圖片
        y = bitmap.getWidth();//初始化y軸坐標(biāo)
    }
  //改變裁剪區(qū)域
    private void playAnimator() {
        if (y > 0) {
            y-=3;
        }
    }
    
    private void drawLoadingAnimator() {
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            if(canvas != null){
                canvas.drawBitmap(bitmap, 100, 100,null);
                canvas.drawColor(Color.GREEN);
                canvas.drawBitmap(bitmap, 100, 100, paint2);
                canvas.save();
          //裁剪 canvas.clipRect(
100, y+100, bitmap.getWidth()+100, bitmap.getHeight()+100); canvas.drawBitmap(bitmap, 100, 100, paint1); canvas.restore(); } /* * Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); * Rect dst = new Rect(100, 100, bitmap.getWidth()+100, y+100); * canvas.drawBitmap(bitmap, src, dst, paint2); */ } catch (Exception e) { e.printStackTrace(); } finally { try{ if (holder != null) { holder.unlockCanvasAndPost(canvas); } }catch(Exception e){ e.printStackTrace(); } } } @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) { }   //繪制動(dòng)畫線程 @Override public void run() { while (flag) { drawLoadingAnimator(); playAnimator(); try { Thread.sleep(200); } catch (Exception e) { e.printStackTrace(); } } } }
復(fù)制代碼

LoadingAnimatorActivity.java

復(fù)制代碼
package cn.yw.lib.animation;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;

@SuppressLint("NewApi")
public class LoadingAnimatorActivity extends Activity{
    private LoadingAnimatorView view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = new LoadingAnimatorView(this);
        setContentView(view);
    }
    @Override
    public void onBackPressed() {
        view.flag = false;//結(jié)束繪制線程
        super.onBackPressed();
    }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多