CustomImageView abgerundete ecken

  • Antworten:1
Julian S.
  • Forum-Beiträge: 77

07.01.2014, 17:05:40 via Website

Hallo,

Ich will zu einem ImageView abgerundete ecken hinzufügen. Das klappt auch soweit nur leider nicht in Android > 4.1
Hier ist mein code:
1@Override
2 protected void onDraw(Canvas canvas) {
3 Path clipPath = new Path();
4 float radius = 10.0f;
5 float padding = radius / 2;
6 int w = this.getWidth();
7 int h = this.getHeight();
8 clipPath.addRoundRect(new RectF(padding, padding, w - padding, h - padding), radius, radius, Path.Direction.CW);
9 canvas.clipPath(clipPath);
10 super.onDraw(canvas);
11 }

Antworten
Barbaric Chicken
  • Forum-Beiträge: 66

08.01.2014, 09:30:39 via Website

Hallo,

was möchetst du genau erreichen? Möchtest du das auch das Bild mit abgerundeten Ecken dargestellt wird oder möchtest du blos einen Rahmen mit runden Ecken um ein viereckiges Bild haben?

eine kurze Google Suche führte mich auf diesen Blog:
http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

Hier wird nicht die Image View sondern das Image selbst mit runden Ecken versehen.

1BitmapShader shader;
2shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
3
4Paint paint = new Paint();
5paint.setAntiAlias(true);
6paint.setShader(shader);
7
8RectF rect = new RectF(0.0f, 0.0f, width, height);
9
10// rect contains the bounds of the shape
11// radius is the radius in pixels of the rounded corners
12// paint contains the shader that will texture the shape
13canvas.drawRoundRect(rect, radius, radius, paint);

Für einen runden Rahmen um das ursprüngliche Bild kannst du dir im drawable Ordner einfach eine Datei machen:
1<?xml version="1.0" encoding="utf-8"?>
2<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3
4 <item
5 android:bottom="0dp"
6 android:left="0dp"
7 android:right="0dp"
8 android:top="0dp"
9 >
10
11 <shape android:shape="rectangle" >
12 <stroke android:width="1dp"
13 android:color="#ff000000"/>
14
15 <padding android:left="4dp"
16 android:top="4dp"
17 android:right="4dp"
18 android:bottom="4dp"/>
19
20 <corners android:radius="15px"/>
21 </shape>
22 </item>
23</layer-list>

und bei deiner ImageView dann den Background (ob aus code oder xml) auf diese Datei setzen.

Viele Grüße

Antworten