-
Notifications
You must be signed in to change notification settings - Fork 2
/
Practice01ClipRectView.java
50 lines (36 loc) · 1.28 KB
/
Practice01ClipRectView.java
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
package com.geekholt.practiceui.view.lesson4;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.geekholt.practiceui.R;
public class Practice01ClipRectView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap bitmap;
public Practice01ClipRectView(Context context) {
super(context);
}
public Practice01ClipRectView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Practice01ClipRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.maps);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int left = (getWidth() - bitmap.getWidth()) / 2;
int top = (getHeight() - bitmap.getHeight()) / 2;
canvas.save();
canvas.clipRect(left + 50, top + 50, left + 300, top + 200);
canvas.drawBitmap(bitmap, left, top, paint);
canvas.restore();
}
}