前言
在Android音视频开发中,网上知识点过于零碎,自学起来难度非常大,不过音视频大牛Jhuster提出了《Android 音视频从入门到提高 - 任务列表》。本文是Android音视频任务列表的第一篇, 对应的要学习的内容是:在Android平台绘制一张图片,使用至少3种不同的 API,ImageView,SurfaceView,自定义View。
音视频任务列表
音视频任务列表: 点击此处跳转查看.
(一)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>
(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)结果:
(二)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)结果:
(三) 自定义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();
}
}
}