快捷搜索:  汽车  科技

imageview使用教学(通过三种方式ImageViewSurfaceView)

imageview使用教学(通过三种方式ImageViewSurfaceView)// 方式一:从手机读取图片 // 方式二:从mipmap-hdpi读取图片 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 如果从手机读取图片,则需要申请权限 verifyStoragePermissions(this); setContentView(R.layout.activity_main); } private static final int REQUEST_EXTERNAL_STORAGE = 1; priv

本文是Android音视频任务列表的第一篇, 对应的要学习的内容是:在Android平台绘制一张图片,使用至少3种不同的 API,ImageView,SurfaceView,自定义View。

目录

imageview使用教学(通过三种方式ImageViewSurfaceView)(1)

(一)ImageView绘制图片(1)将图片分别放入mipmap-hdpi、assets(2)布局文件:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@ id/iv_picture" android:layout_width="match_parent" android:layout_height="wrap_content"> </ImageView> </LinearLayout>

【相关学习资料推荐,点击下方链接免费报名,免费报名后会弹出学习资料领取地址~】

【免费】FFmpeg/WebRTC/RTMP/NDK/Android音视频流媒体高级开发-学习视频教程-腾讯课堂

(3)代码:

// ImageView加载几种来源: // (1)drawable/mipmap中通过R.drawabe.xxx加载图片资源 // (2)assests路径的资源 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView customImageView = findViewById(R.id.iv_picture); // 方式一:drawable/mipmap中通过R.drawabe.xxx加载图片资源 // customImageView.setBackgroundResource(R.mipmap.wuqian); // 方式二:加载assests路径的资源 customImageView.setImagebitmap(getImageFromAssetsFile(this "lilangdi.jpg")); } public static Bitmap getImageFromAssetsFile(Context context String fileName) { Bitmap image = null; AssetManager am = context.getResources().getAssets(); try { InputStream is = am.open(fileName); image = BitmapFactory.decodeStream(is); is.close(); } catch(IOException e) { e.printStackTrace(); } return image; } }(4)结果:

imageview使用教学(通过三种方式ImageViewSurfaceView)(2)

(二)SurfaceView绘制图片(1)将图片放入手机文件夹内(2)布局:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <SurfaceView android:id="@ id/sv_drawpicture" android:layout_width="match_parent" android:layout_height="match_parent"> </SurfaceView> </LinearLayout>(3)添加权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>(4)代码:

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); verifyStoragePermissions(this); setContentView(R.layout.Activity_main); SurfaceView mSurfaceView = findViewById(R.id.sv_drawpicture); mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { if (holder == null) { return; } Paint paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); // 方式一:Environment.getExternalStorageDirectory().getPath()获取路径为:/storage/emulated/0 // File.separator得到的是:/ Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() File.separator "wuqian.jpg"); // 获取bitmap // 方式二: // Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/0/wuqian.jpg"); // 获取bitmap Canvas canvas = holder.lockCanvas(); // 先锁定当前surfaceView的画布 canvas.drawBitmap(bitmap 0 0 paint); //执行绘制操作 holder.unlockCanvasAndPost(canvas); // 解除锁定并显示在界面上 } @Override public void surfaceChanged(SurfaceHolder holder int format int width int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }); } private static final int request_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { "android.permission.READ_EXTERNAL_STORAGE" "android.permission.WRITE_EXTERNAL_STORAGE" }; public static void verifyStoragePermissions(Activity activity) { try { // 检测是否有写的权限 int permission = ActivityCompat.checkSelfPermission(activity "android.permission.WRITE_EXTERNAL_STORAGE"); if (permission != PackageManager.PERMISSION_GRANTED) { // 没有写的权限,去申请写的权限,会弹出对话框 ActivityCompat.requestPermissions(activity PERMISSIONS_STORAGE REQUEST_EXTERNAL_STORAGE); } } catch (Exception e) { e.printStackTrace(); } } }(5)结果:

imageview使用教学(通过三种方式ImageViewSurfaceView)(3)

【相关学习资料推荐,点击下方链接免费报名,报名后会弹出学习资料免费领取地址~】

【免费】FFmpeg/WebRTC/RTMP/NDK/Android音视频流媒体高级开发-学习视频教程-腾讯课堂

(三) 自定义View绘制图片(1)将图片放入手机或者放入mipmap-hdpi(2)布局:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.lzacking.viewdemo.CustomView android:id="@ id/customview" android:layout_width="match_parent" android:layout_height="match_parent"> </com.lzacking.viewdemo.CustomView> </LinearLayout>(3)添加权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />(4)代码:

CustomView:

public class CustomView extends View { Paint paint = new Paint(); Bitmap bitmap; public CustomView(Context context @Nullable AttributeSet attrs) { super(context attrs); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); // 方式一:从手机读取图片 bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() File.separator "kugou.png"); // 获取bitmap // 方式二:从mipmap-hdpi读取图片 // bitmap = BitmapFactory.decodeResource(getResources() R.mipmap.taylor); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 不建议在onDraw做任何分配内存的操作 if (bitmap != null) { canvas.drawBitmap(bitmap 0 0 paint); } } }

MainActivity:

// 方式一:从手机读取图片 // 方式二:从mipmap-hdpi读取图片 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 如果从手机读取图片,则需要申请权限 verifyStoragePermissions(this); setContentView(R.layout.activity_main); } private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { "android.permission.READ_EXTERNAL_STORAGE" "android.permission.WRITE_EXTERNAL_STORAGE" }; public static void verifyStoragePermissions(Activity activity) { try { // 检测是否有写的权限 int permission = ActivityCompat.checkSelfPermission(activity "android.permission.WRITE_EXTERNAL_STORAGE"); if (permission != PackageManager.PERMISSION_GRANTED) { // 没有写的权限,去申请写的权限,会弹出对话框 ActivityCompat.requestPermissions(activity PERMISSIONS_STORAGE REQUEST_EXTERNAL_STORAGE); } } catch (Exception e) { e.printStackTrace(); } } }(5)结果:

imageview使用教学(通过三种方式ImageViewSurfaceView)(4)

如果你对音视频开发感兴趣,觉得文章对您有帮助,别忘了点赞、收藏哦!或者对本文的一些阐述有自己的看法,有任何问题,欢迎在下方评论区与我讨论!

猜您喜欢: