I'm trying to apply transformation to an image view using Matrix
class. My idea is to be able to mirror the image, but actually any transformation I apply (e.g. scale or translate) doesn't work. I tried creating a custom view and add the code in onPreDraw
event placed in the constructor, also tried on click event - non of them worked. My code is as follows:
Image drawable I use
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=";
android:shape="oval">
<solid android:color="@color/bright_sun" />
</shape>
Setting the matrix (either in onPreDraw
or on click)
val m = Matrix()
m.setScale(0.5f, 0.5f)
//m.setTranslate(30f, 30f) // doesn't work either
setImageMatrix(m)
binding.sun.invalidate()
XML code for ImageView
It's nested in a FrameLayout
which is part of vertical LinearLayout
. The frame layout has android:layout_weight
applied. Other than that, nothing special.
<ImageView
android:id="@+id/sun"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:scaleType="matrix"
android:src="@drawable/sun" />
Tried all sorts of combinations (specifying scaleType
and drawable in both code and XML), but with no success. Code is based on this article I encountered.
I'm trying to apply transformation to an image view using Matrix
class. My idea is to be able to mirror the image, but actually any transformation I apply (e.g. scale or translate) doesn't work. I tried creating a custom view and add the code in onPreDraw
event placed in the constructor, also tried on click event - non of them worked. My code is as follows:
Image drawable I use
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android/apk/res/android"
android:shape="oval">
<solid android:color="@color/bright_sun" />
</shape>
Setting the matrix (either in onPreDraw
or on click)
val m = Matrix()
m.setScale(0.5f, 0.5f)
//m.setTranslate(30f, 30f) // doesn't work either
setImageMatrix(m)
binding.sun.invalidate()
XML code for ImageView
It's nested in a FrameLayout
which is part of vertical LinearLayout
. The frame layout has android:layout_weight
applied. Other than that, nothing special.
<ImageView
android:id="@+id/sun"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:scaleType="matrix"
android:src="@drawable/sun" />
Tried all sorts of combinations (specifying scaleType
and drawable in both code and XML), but with no success. Code is based on this article I encountered.
1 Answer
Reset to default 1As suggested by @Mike M's comment, I had to just add
<size android:width="100dp" android:height="100dp" />
to the drawable to get it working.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745159243a4614310.html
ImageView
. If you add<size android:width="100dp" android:height="100dp" />
, for example, your matrix stuff should start working. – Mike M. Commented Feb 23 at 10:32