0%

音视频任务1

跟着大神的步骤一步一步做,加油!
任务列表:http://blog.51cto.com/ticktick/1956269

在 Android 平台绘制一张图片,使用至少 3 种不同的 API,ImageView,SurfaceView,自定义 View
imageView和surfaceView,为了方便都是直接在activity里面写的函数,直接在onCreateView里面调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displaySurfaceView();
}

void displayImg(){
final ImageView iv = findViewById(R.id.iv_lu);
//1
// iv.setImageResource(R.mipmap.wechatimg1);

//2
// Drawable drawable = getResources().getDrawable(R.mipmap.wechatimg1, null);
// iv.setImageDrawable(drawable);
//3
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.wechatimg1);
iv.setImageBitmap(bitmap);
}

void displaySurfaceView(){
final SurfaceView sv = findViewById(com.example.lujianyun.va.R.id.sv_lu);

final SurfaceHolder holder = sv.getHolder();
final Thread t = new Thread(new Runnable() {
@Override
public void run() {
Canvas canvas = holder.lockCanvas();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), com.example.lujianyun.va.R.mipmap.wechatimg1);
canvas.drawBitmap(bitmap, 0, 0, null);
holder.unlockCanvasAndPost(canvas);
holder.lockCanvas(new Rect(0,0,0,0));
holder.unlockCanvasAndPost(canvas);
}
});

holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
t.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
});



}
}

自定义view

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class DrawView extends View {

String TAG = "DrawView";
Bitmap bitmap;
Bitmap orginBitmap;
boolean bitmapFinished = false;

public DrawView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
orginBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.wechatimg1);


}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!bitmapFinished) { //由于onmeasure要多次调用,所以创建对象的工作只做一次,防止耗时
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
bitmap = Bitmap.createScaledBitmap(orginBitmap, w, h, false);
bitmapFinished = true;
}
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bitmap != null)
canvas.drawBitmap(bitmap, 0, 0, null);
}

}

布局文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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="300dp"
android:layout_height="300dp"
tools:context=".MainActivity"
android:clipChildren="false">

<ImageView
android:id="@+id/iv_lu"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<SurfaceView
android:id="@+id/sv_lu"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.example.lll.va.DrawView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>