当前位置:操作系统 > 安卓/Android >>

Su易做图ceView绘图机制

虽然前面都使用的View来进行绘图,但View绘图机制存在两个缺陷:

1、缺乏双缓冲机制

2、更新图像时,必须更新View上的整张图片,效率低。

所以在游戏绘制中推荐使用Su易做图ceView。它可以只对指定区域进行更新,可以提高效率。下面用一个简单示例来演示Su易做图ceView的使用:

Activity:

 

package com.home.activity; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.Su易做图ceHolder; 
import android.view.Su易做图ceHolder.Callback; 
import android.view.Su易做图ceView; 
import android.view.View; 
import android.view.View.OnTouchListener; 
 
import com.home.su易做图ceviewtest.R; 
 
public class Su易做图ceViewActivity extends Activity { 
    // Su易做图ceHolder负责维护Su易做图ceView上绘制的内容  
    private Su易做图ceHolder holder; 
    private Paint paint; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        paint = new Paint(); 
        // 获取Su易做图ceView实例  
        Su易做图ceView su易做图ce = (Su易做图ceView) findViewById(R.id.main_sv); 
        // 初始化Su易做图ceHolder对象  
        holder = su易做图ce.getHolder(); 
        holder.addCallback(new Callback() { 
            //当su易做图ce将要被销毁时回调该方法  
            @Override 
            public void su易做图ceDestroyed(Su易做图ceHolder holder) { 
 
            } 
            //当su易做图ce被创建时回调该方法  
            @Override 
            public void su易做图ceCreated(Su易做图ceHolder holder) { 
                // 锁定整个Su易做图ceView  
                Canvas canvas = holder.lockCanvas(); 
                // 获取背景资源  
                Bitmap bitmap = BitmapFactory.decodeResource( 
                        Su易做图ceViewActivity.this.getResources(), 
                        R.drawable.image2); 
                // 绘制背景  
                canvas.drawBitmap(bitmap, 0, 0, null); 
                // 绘制完成,释放画布,提交修改  
                holder.unlockCanvasAndPost(canvas); 
                // 重新锁两次,避免下次lockCanvas遮挡  
                holder.lockCanvas(new Rect(0, 0, 0, 0)); 
                holder.unlockCanvasAndPost(canvas); 
                holder.lockCanvas(new Rect(0, 0, 0, 0)); 
                holder.unlockCanvasAndPost(canvas); 
            } 
            //当一个su易做图ce的格式或大小发生改变时回调该方法  
            @Override 
            public void su易做图ceChanged(Su易做图ceHolder holder, int format, 
                    int width, int height) { 
 
            } 
        }); 
        su易做图ce.setOnTouchListener(new OnTouchListener() { 
 
            @Override 
            public boolean onTouch(View v, MotionEvent event) { 
                // 只处理按下事件  
                if (event.getAction() == MotionEvent.ACTION_DOWN) { 
                    int cx = (int) event.getX(); 
                    int cy = (int) event.getY(); 
                    // 锁定Su易做图ceView的局部区域,只更新局部内容  
                    Canvas canvas = holder.lockCanvas(new Rect(cx - 60, 
                            cy - 60, cx + 60, cy + 60)); 
                    // 保存canvas的当前状态  
                    canvas.save(); 
                    // 旋转画布  
                    canvas.rotate(30, cx, cy); 
                    paint.setColor(Color.RED); 
                    // 绘制红色方块  
                    canvas.drawRect(cx - 40, cy - 40, cx, cy, paint); 
                    // 恢复canvas之前的保存状态  
                    canvas.restore(); 
                    paint.setColor(Color.GREEN); 
                    // 绘制绿色方块  
                    canvas.drawRect(cx, cy, cx + 40, cy + 40, paint); 
                    // 绘制完成,释放画布,提交修改  
                    holder.unlockCanvasAndPost(canvas); 
                } 
                return false; 
            } 
        }); 
    } 
 
} 

package com.home.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceHolder.Callback;
import android.view.Su易做图ceView;
import android.view.View;
import android.view.View.OnTouchListener;

import com.home.su易做图ceviewtest.R;

public class Su易做图ceViewActivity extends Activity {
 // Su易做图ceHolder负责维护Su易做图ceView上绘制的内容
 private Su易做图ceHolder holder;
 private Paint paint;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  paint = new Paint();
  // 获取Su易做图ceView实例
  Su易做图ceView su易做图ce = (Su易做图ceView) findViewById(R.id.main_sv);
  // 初始化Su易做图ceHolder对象
  holder = su易做图ce.getHolder();
  holder.addCallback(new Callback() {
   //当su易做图ce将要被销毁时回调该方法
   @Override
   public void su易做图ceDestroyed(Su易做图ceHolder holder) {

   }
   //当su易做图ce被创建时回调该方法
   @Override
   public void su易做图ceCreated(Su易做图ceHolder holder) {
    // 锁定整个Su易做图ceView
    Canvas canvas = holder.lockCanvas();
    // 获取背景资源
    Bitmap bitmap = BitmapFactory.decodeResource(
      Su易做图ceViewActivity.this.getResources(),
      R.drawable.image2);
    // 绘制背景
    canvas.drawBitmap(bitmap, 0, 0, null);
    // 绘制完成,释放画布,提交修改
    holder.unlockCanvasAndPost(canvas);
    // 重新锁两次,避免下次lockCanvas遮挡
    holder.lockCanvas(new Rect(0, 0, 0, 0));
    holder.unlockCanvasAndPost(canvas);
    holder.lockCanvas(new Rect(0, 0, 0, 0));
    holder.unlockCanvasAndPost(canvas);
   }
   //当一个su易做图ce的格式或大小发生改变时回调该方法
   @Override
   public void su易做图ceChanged(Su易做图ceHolder holder, int format,
     int width, int height) {

   }
  });
  su易做图ce.setOnTouchListener(new OnTouchListener() {

   @Override
   public boolean onTouch(View v, MotionEvent event) {
    // 只处理按下事件
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
     int cx = (int) event.getX();
     int cy = (int) event.getY();
     // 锁定Su易做图ceView的局部区域,只更新局部内容
     Canvas canvas = holder.lockCanvas(new Rect(cx - 60,
       cy - 60, cx + 60, cy + 60));
     // 保存canvas的当前状态
     canvas.save();
     // 旋转画布
     canvas.rotate(30, cx, cy);
     paint.setColor(Color.RED);
     // 绘制红色方块
     canvas.drawRect(cx - 40, cy - 40, cx, cy, paint);
     // 恢复canvas之前的保存状态
     canvas.restore();
     paint.setColor(Color.GREEN);
     // 绘制绿色方块
     canvas.drawRect(cx, cy, cx + 40, cy + 40, paint);
     // 绘制完成,释放画布,提交修改
     holder.unlockCanvasAndPost(canvas);
    }
    return false;
   }
  });
 }

}

 


布局XML:


 

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
 
    <Su易做图ceView 
        android:id="@+id/main_sv" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"/> 
 
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Su易做图ceView
        android:id="@+id/main_sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

 

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,