Android Image View's setImageMatrix not working - Stack Overflow

I'm trying to apply transformation to an image view using Matrix class. My idea is to be able to m

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.

Share Improve this question asked Feb 23 at 9:27 RokataRokata 1,25715 silver badges34 bronze badges 4
  • Your drawable doesn't have a set size, so there's not really anything to do matrix calculations against, and it's just measured to fit the 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
  • Wow, I though about it, but didn't try it out. Thanks! Can you post it as an answer so I can accept it? – Rokata Commented Feb 23 at 11:05
  • 1 No problem! I don't really post answers here anymore, though. Please feel free to finish this up however you like. Thank you, I appreciate the offer. Glad you got it working. Cheers! – Mike M. Commented Feb 23 at 11:11
  • 1 I posted it as an answer and will accept it in 2 days so others can see the official solution. Thanks again and cheers! :) – Rokata Commented Feb 23 at 11:56
Add a comment  | 

1 Answer 1

Reset to default 1

As 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

相关推荐

  • Android Image View&#39;s setImageMatrix not working - Stack Overflow

    I'm trying to apply transformation to an image view using Matrix class. My idea is to be able to m

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信