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

Android双Su易做图ceView实现底部拍照,顶部绘图

当Su易做图ceHolder对象的类型设置为Su易做图ceHolder.SURFACE_TYPE_PUSH_BUFFERS时就只能拍照不能绘制了。
为了既能通过Su易做图ceView拍照又能在上面绘制图形,可以通过双Su易做图ceView层叠的变通方式如下:
用于绘制的Su易做图ceView,使其透明并位于顶部:
[java]
package com.test; 
 
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.graphics.PixelFormat; 
import android.graphics.Rect; 
import android.graphics.Paint.Style; 
import android.util.AttributeSet; 
import android.view.Su易做图ceHolder; 
import android.view.Su易做图ceView; 
 
public class SVDraw extends Su易做图ceView implements Su易做图ceHolder.Callback { 
 
    private Bitmap bmp; 
    private String imgPath = ""; 
    protected Su易做图ceHolder sh; // 专门用于控制su易做图ceView的 
    private int width; 
    private int height; 
 
    // XML文件解析需要调用View的构造函数View(Context , AttributeSet) 
    // 因此自定义Su易做图ceView中也需要该构造函数 
    public SVDraw(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        // TODO Auto-generated constructor stub 
        sh = getHolder(); 
        sh.addCallback(this); 
        sh.setFormat(PixelFormat.TRANSPARENT); // 设置为透明 
        setZOrderOnTop(true);// 设置为顶端 
    } 
 
    @Override 
    public void su易做图ceChanged(Su易做图ceHolder arg0, int arg1, int w, int h) { 
        // TODO Auto-generated method stub 
        width = w; 
        height = h; 
    } 
 
    @Override 
    public void su易做图ceCreated(Su易做图ceHolder arg0) { 
        // TODO Auto-generated method stub 
 
    } 
 
    @Override 
    public void su易做图ceDestroyed(Su易做图ceHolder arg0) { 
        // TODO Auto-generated method stub 
 
    } 
 
    void clearDraw() { 
 
        Canvas canvas = sh.lockCanvas(); 
        canvas.drawColor(Color.BLUE);// 清除画布 
        sh.unlockCanvasAndPost(canvas); 
    } 
 
    /**
     * 绘制
     */ 
    public void doDraw() { 
        if (bmp != null) { 
            Canvas canvas = sh.lockCanvas(); 
            canvas.drawColor(Color.TRANSPARENT);// 这里是绘制背景 
            Paint p = new Paint(); // 笔触 
            p.setAntiAlias(true); // 反锯齿 
            p.setColor(Color.RED); 
            p.setStyle(Style.STROKE); 
            canvas.drawBitmap(bmp, 0, 0, p); 
            canvas.drawLine(width / 2 - 100, 0, width / 2 - 100, height, p); 
            canvas.drawLine(width / 2 + 100, 0, width / 2 + 100, height, p); 
            // ------------------------ 画边框--------------------- 
            Rect rec = canvas.getClipBounds(); 
            rec.bottom--; 
            rec.right--; 
            p.setColor(Color.GRAY); // 颜色 
            p.setStrokeWidth(5); 
            canvas.drawRect(rec, p); 
            // 提交绘制 
            sh.unlockCanvasAndPost(canvas); 
        } 
 
    } 
 
    public void drawLine() { 
 
        Canvas canvas = sh.lockCanvas(); 
 
        canvas.drawColor(Color.TRANSPARENT);// 这里是绘制背景 
        Paint p = new Paint(); // 笔触 
        p.setAntiAlias(true); // 反锯齿 
        p.setColor(Color.RED); 
        p.setStyle(Style.STROKE); 
        canvas.drawLine(width / 2 - 100, 0, width / 2 - 100, height, p); 
        canvas.drawLine(width / 2 + 100, 0, width / 2 + 100, height, p); 
 
        // 提交绘制 
        sh.unlockCanvasAndPost(canvas); 
    } 
 
    public String getImgPath() { 
        return imgPath; 
    } 
 
    public void setImgPath(String imgPath) { 
        this.imgPath = imgPath; 
        // 根据路径载入目标图像 
        bmp = BitmapFactory.decodeFile(imgPath); 
    } 
 

用于在Su易做图ceView(使其位于绘制Su易做图ceView底部)上拍照及预览的Activity:
[java] 
package com.test; 
 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Iterator; 
import java.util.List; 
 
import android.app.Activity; 
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,