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

android图像绘制(四)——Su易做图ceView问题,自定义控件

自定义控件(类似按钮等)的使用,自定义一个Su易做图ceView。

如某一块的动态图(自定义相应),或者类似UC浏览器下面的工具栏。

如下图示例:

 \

 

自定义类代码:


[java]
public class ImageSu易做图ceView extends Su易做图ceView implements Callback{ 
    //用于控制Su易做图ceView  
    private Su易做图ceHolder sfh; 
    private Handler handler = new Handler(); 
    private ImageRunnable imageRunnable = new ImageRunnable(); 
    private Paint paint; 
    private Canvas canvas; 
    private Matrix matrix; 
     
    /**图片的坐标*/ 
    private float imageX, imageY; 
    /**获取的图片*/ 
    private Bitmap bmp; 
    /**图片宽高*/ 
    private float bmpW, bmpH; 
    /**屏幕大小*/ 
    private int screenW, screenH; 
 
    /**
     * Su易做图ceView初始化函数
     */ 
    public ImageSu易做图ceView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        sfh = this.getHolder(); 
        sfh.addCallback(this); 
        paint = new Paint(); 
        paint.setColor(Color.WHITE); 
        paint.setAntiAlias(true); 
        setFocusable(true); 
    } 
    /**
     * Su易做图ceView视图创建,响应此函数
     */ 
    @Override 
    public void su易做图ceCreated(Su易做图ceHolder holder) { 
        System.out.println("ImageSu易做图ceView is su易做图ceCreated"); 
        screenH = this.getHeight(); 
        screenW = this.getWidth(); 
                handler.post(imageRunnable); 
    } 
    /**
     * 游戏绘图
     */ 
    public void draw() { 
        try { 
            canvas = sfh.lockCanvas(); 
            canvas.drawRGB(0, 0, 0); 
            canvas.save(); 
            //绘制  
            canvas.drawBitmap(bmp, matrix, paint); 
            System.out.println("绘制图像了吗?"); 
            canvas.restore(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            if (canvas != null) 
                sfh.unlockCanvasAndPost(canvas); 
        } 
    } 
    /**
     * 触屏事件监听
     */ 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
        return true; 
    } 
     
    /**
     * 图片的线程运行
     */ 
    class ImageRunnable implements Runnable{ 
        @Override 
        public void run() { 
            long start = System.currentTimeMillis(); 
            draw(); 
            long end = System.currentTimeMillis(); 
            if (end - start < 500) { 
                handler.postDelayed(this, 200 - (end-start)); 
            }else{ 
                handler.post(this); 
            } 
        } 
    } 
     
    /**
     * Su易做图ceView视图状态发生改变,响应此函数
     */ 
    @Override 
    public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width, int height) { 
        System.out.println("ImageSu易做图ceView is su易做图ceChanged"); 
    } 
    /**
     * Su易做图ceView视图消亡时,响应此函数
     */ 
    @Override 
    public void su易做图ceDestroyed(Su易做图ceHolder holder) { 
        System.out.println("ImageSu易做图ceView is su易做图ceDestroyed"); 
    } 

public class ImageSu易做图ceView extends Su易做图ceView implements Callback{
 //用于控制Su易做图ceView
 private Su易做图ceHolder sfh;
 private Handler handler = new Handler();
 private ImageRunnable imageRunnable = new ImageRunnable();
 private Paint paint;
 private Canvas canvas;
 private Matrix matrix;
 
 /**图片的坐标*/
 private float imageX, imageY;
 /**获取的图片*/
 private Bitmap bmp;
 /**图片宽高*/
 private float bmpW, bmpH;
 /**屏幕大小*/
 private int screenW, screenH;

 /**
  * Su易做图ceView初始化函数
  */
 public ImageSu易做图ceView(Context context, AttributeSet attrs) {
  super(context, attrs);
  sfh = this.getHolder();
  sfh.addCallback(this);
  paint = new Paint();
  paint.setColor(Color.WHITE);
  paint.setAntiAlias(true);
  setFocus

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