Skip to content

Commit 98209d5

Browse files
committed
Add ClipRectActivity
1 parent 39af95e commit 98209d5

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

app/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
android:name=".PeekThroughActivity"
2828
android:label="@string/peek_through"
2929
android:parentActivityName=".MainActivity" />
30+
<activity
31+
android:name=".ClipRectActivity"
32+
android:label="@string/clip_rect"
33+
android:parentActivityName=".MainActivity" />
3034
<activity
3135
android:name=".ColorMatrixActivity"
3236
android:label="@string/color_matrix"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.sqisland.android.graphics_demo;
2+
3+
import android.app.Activity;
4+
import android.graphics.Bitmap;
5+
import android.graphics.Canvas;
6+
import android.graphics.Color;
7+
import android.graphics.Paint;
8+
import android.graphics.RectF;
9+
import android.os.Bundle;
10+
import android.widget.ImageView;
11+
12+
public class ClipRectActivity extends Activity {
13+
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
14+
private RectF rect = new RectF();
15+
16+
@Override
17+
protected void onCreate(Bundle savedInstanceState) {
18+
super.onCreate(savedInstanceState);
19+
setContentView(R.layout.activity_clip_rect);
20+
21+
ImageView imageView = (ImageView) findViewById(R.id.image);
22+
int width = getResources().getDimensionPixelSize(R.dimen.clip_rect_width);
23+
int height = getResources().getDimensionPixelSize(R.dimen.clip_rect_height);
24+
25+
Bitmap bitmap = createClipRect(width, height);
26+
imageView.setImageBitmap(bitmap);
27+
}
28+
29+
// Assumes width > height
30+
private Bitmap createClipRect(int width, int height) {
31+
paint.setColor(Color.GREEN);
32+
33+
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
34+
Canvas canvas = new Canvas(bitmap);
35+
float radius = height / 2;
36+
37+
canvas.save();
38+
canvas.clipRect(0, 0, width, height);
39+
40+
rect.set(0, 0, width + height, height);
41+
canvas.drawRoundRect(rect, radius, radius, paint);
42+
43+
canvas.restore();
44+
45+
return bitmap;
46+
}
47+
}

app/src/main/java/com/sqisland/android/graphics_demo/MainActivity.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected void onCreate(Bundle savedInstanceState) {
2727
demos.add(new Demo(this, RainbowGradientActivity.class, R.string.rainbow_gradient));
2828
demos.add(new Demo(this, PatternedTextActivity.class, R.string.patterned_text));
2929
demos.add(new Demo(this, PeekThroughActivity.class, R.string.peek_through));
30+
demos.add(new Demo(this, ClipRectActivity.class, R.string.clip_rect));
3031
demos.add(new Demo(this, ColorMatrixActivity.class, R.string.color_matrix));
3132
demos.add(new Demo(this, FourColorsActivity.class, R.string.four_colors));
3233
demos.add(new Demo(this, PorterDuffActivity.class, R.string.porter_duff));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<ImageView
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/image"
5+
android:layout_width="wrap_content"
6+
android:layout_height="wrap_content"
7+
android:layout_gravity="center"
8+
tools:context=".ClipRectActivity"/>

app/src/main/res/values/dimens.xml

+3
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88
<dimen name="peek_through_radius">40dp</dimen>
99

1010
<dimen name="dashed_text_stroke_width">8dp</dimen>
11+
12+
<dimen name="clip_rect_width">120dp</dimen>
13+
<dimen name="clip_rect_height">40dp</dimen>
1114
</resources>

app/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<string name="peek_through">Peek through</string>
1313
<string name="peek_through_help">Touch the gray area to peek</string>
1414

15+
<string name="clip_rect">Clip rect</string>
16+
1517
<string name="color_matrix">ColorMatrix</string>
1618
<string name="grayscale">Grayscale</string>
1719
<string name="sepia">Sepia</string>

0 commit comments

Comments
 (0)