admin管理员组文章数量:1415655
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.
本文标签: Android Image View39s setImageMatrix not workingStack Overflow
版权声明:本文标题:Android Image View's setImageMatrix not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745159242a2645361.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