Clipping Android View mit Pfad und Umriss in der Android -Version größer als 30
Posted: 18 Mar 2025, 16:25
Ich habe eine AppCompatimageView in einer XML -Layout -Datei. < /p>
Ich habe die Quelle der Ansicht wie folgt festgelegt:android:src="@drawable/nicegradientshape"
< /code>
Die Form ist ein einfaches Rechteck < /p>
Ich habe den Umriss der Ansicht mit einem Umrissobjekt gelesen. Ich habe versucht, dafür einen Weg zu verwenden. Aber es hat nicht funktioniert. Hier ist der Code: < /p>
//inside onViewcreated method of the fragment that hosts the layout where the imageview is found:
view.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
view.setClipToOutline(true);
view.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
//I check the android version first: the setpath method does not work if version < 30
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)return;
//I create a rect object:
Rect rect = new Rect();
float xx1 = view.getWidth()*0.05f;
float xx2 = view.getWidth()*0.15f;
float xx3 = view.getWidth()*0.8f;
float xx4 = view.getWidth()*0.95f;
float yyTop = view.getHeight()*0.05f;
float yyBottom = view.getHeight()*0.95f;
rect.left=(int)xx1;
rect.top=(int)yyTop;
rect.rigth=(int)xx4;
rect.bottom=(int)yyBottom
//the numbers above show in a Log statement as such: 6.3 18.900002 100.8 119.7 6.3 119.7 Rect(6, 6 - 119, 119)
//I create my Path
Path path = new Path(); //should be an inverted triangle!!
path.moveTo(rect.centerX(),rect.centerY());
path.lineTo(rect.left,rect.top);
path.lineTo(rect.right,rect.top);
path.close();
outline.setPath(path);
}
< /code>
Der Pfad wird nicht in die ImageView abgeschnitten. jedoch: Einstellung wie diese funktioniert gut:
outline.setOval(new Rect((int)xx1,(int)yyTop,(int)xx4,(int)yyBottom));
< /code>
Was könnte in meinem Weg los sein? Warum wird die Ansicht nicht mit dem Pfad abgeschnitten? Nur mit der SetVal -Methode?
Danke für die Hilfe
Ich habe die Quelle der Ansicht wie folgt festgelegt:android:src="@drawable/nicegradientshape"
< /code>
Die Form ist ein einfaches Rechteck < /p>
Ich habe den Umriss der Ansicht mit einem Umrissobjekt gelesen. Ich habe versucht, dafür einen Weg zu verwenden. Aber es hat nicht funktioniert. Hier ist der Code: < /p>
//inside onViewcreated method of the fragment that hosts the layout where the imageview is found:
view.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
view.setClipToOutline(true);
view.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
//I check the android version first: the setpath method does not work if version < 30
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)return;
//I create a rect object:
Rect rect = new Rect();
float xx1 = view.getWidth()*0.05f;
float xx2 = view.getWidth()*0.15f;
float xx3 = view.getWidth()*0.8f;
float xx4 = view.getWidth()*0.95f;
float yyTop = view.getHeight()*0.05f;
float yyBottom = view.getHeight()*0.95f;
rect.left=(int)xx1;
rect.top=(int)yyTop;
rect.rigth=(int)xx4;
rect.bottom=(int)yyBottom
//the numbers above show in a Log statement as such: 6.3 18.900002 100.8 119.7 6.3 119.7 Rect(6, 6 - 119, 119)
//I create my Path
Path path = new Path(); //should be an inverted triangle!!
path.moveTo(rect.centerX(),rect.centerY());
path.lineTo(rect.left,rect.top);
path.lineTo(rect.right,rect.top);
path.close();
outline.setPath(path);
}
< /code>
Der Pfad wird nicht in die ImageView abgeschnitten. jedoch: Einstellung wie diese funktioniert gut:
outline.setOval(new Rect((int)xx1,(int)yyTop,(int)xx4,(int)yyBottom));
< /code>
Was könnte in meinem Weg los sein? Warum wird die Ansicht nicht mit dem Pfad abgeschnitten? Nur mit der SetVal -Methode?
Danke für die Hilfe