android图像绘制(四)自定义一个SurfaceView控件
时间:2022-09-27 11:28:07
自定义控件(类似按钮等)的使用,自定义一个SurfaceView。
如某一块的动态图(自定义相应),或者类似UC浏览器下面的工具栏。
如下图示例:
自定义类代码:
public class ImageSurfaceView extends SurfaceView implements Callback{
//用于控制SurfaceView
private SurfaceHolder 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;
/**
* SurfaceView初始化函数
*/
public ImageSurfaceView(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);
}
/**
* SurfaceView视图创建,响应此函数
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("ImageSurfaceView is surfaceCreated");
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);
}
}
}
/**
* SurfaceView视图状态发生改变,响应此函数
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
System.out.println("ImageSurfaceView is surfaceChanged");
}
/**
* SurfaceView视图消亡时,响应此函数
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("ImageSurfaceView is surfaceDestroyed");
}
}
layout的xml代码如下(使用方法,类的全地址做为控件名):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<akai.test.getImage.ImageSurfaceView android:id="@+id/myImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<LinearLayout android:id="@+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/white"
>
<Button android:id="@+id/getImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择图片"
/>
<Button android:id="@+id/getImage_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
/>
<Button android:id="@+id/getImage_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
/>
</LinearLayout>
</FrameLayout>
以上代码为例子,仅供参考!
注意以下问题:
1、本类的初始化函数需要加入参数,为:public ImageSurfaceView(Context context, AttributeSet attrs) ;
2、不要在初始化的时候获取screen的宽度和高度,在初始化的时候并还没有执行SurfaceCreated,所以获取宽度和高度要在surfaceCreated或者之后,且在surfaceDestroyed之前;
3、在显示本控件的时候,会执行surfaceCreated和surfaceChanged,当跳转到其他界面的时候则执行surfaceDestroyed(不管是否当前的activity已经销毁),所以如果在跳转回到次控件的时候立刻执行sfh.lockCanvas()的话将会获得空值Null。
标签:android图像绘制,SurfaceView
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 7.0应用之间如何共享文件
2021-09-18 08:43:15
mvc开启gzip压缩示例分享
2022-05-03 08:34:29
值得收藏的2017年Java开发岗位面试题
2023-11-29 15:22:01
深入理解Android Bitmap
2023-07-29 08:52:14
android view转Bitmap生成截图的方法
2022-11-07 13:41:53
Java日常练习题,每天进步一点点(46)
2023-10-05 11:20:29
![](https://img.aspxhome.com/file/2023/0/116190_0s.png)
Android WebView实现截长图功能
2023-07-06 16:11:05
![](https://img.aspxhome.com/file/2023/2/139502_0s.gif)
SpringMVC文件上传 多文件上传实例
2023-05-01 02:25:31
java -jar设置添加启动参数实现方法
2022-04-26 21:05:37
Android App仿QQ制作Material Design风格沉浸式状态栏
2021-06-07 01:10:48
![](https://img.aspxhome.com/file/2023/9/130299_0s.jpg)
Java如果在try里面执行return还会不会执行finally
2022-03-01 18:16:28
C# WinForm实现自动更新程序的方法详解
2021-12-12 16:19:54
![](https://img.aspxhome.com/file/2023/7/106407_0s.gif)
Android studio设计简易计算器
2023-01-26 22:21:40
![](https://img.aspxhome.com/file/2023/2/138282_0s.jpg)
Android实现环形进度条代码
2023-06-08 11:09:55
![](https://img.aspxhome.com/file/2023/9/138129_0s.jpg)
spring boot使用sharding jdbc的配置方式
2022-02-16 00:29:15
c#使用ManagedWifi查看当前Wifi信号并选择wifi的示例
2021-07-06 15:37:18
android仿iphone主题效果的主菜单
2023-04-29 03:56:35
![](https://img.aspxhome.com/file/2023/0/138760_0s.jpg)
Android编程实现自定义ProgressBar样式示例(背景色及一级、二级进度条颜色)
2022-06-15 08:07:54
![](https://img.aspxhome.com/file/2023/9/117149_0s.png)
Java中高效的判断数组中某个元素是否存在详解
2021-05-25 17:32:08
Spring boot集成redis lettuce代码实例
2022-08-03 12:21:19