首页 > 解决方案 > 为什么在android中对视图应用矩阵变换时,不同设备上的矩阵变换不同?

问题描述

我正在使用贴纸视图库https://github.com/wuapnjie/StickerView。我正在借助sticker.getMatrix()方法保存所有贴纸的位置。但是当我将数据库中的值加载到具有不同 DPI 的另一台设备上时,转换就完全不同了。

在进一步研究中,我发现对于翻译,我需要转换 px 来做。所以我将第 2 和第 5 个索引(基于 0 的索引系统)更改为 dp。并在加载值时将其转换回像素

matrix[2] = DensityUtils.px2dp(context, originalmatrix[2]); matrix[5] = DensityUtils.px2dp(context, originalmatrix[5]);

和加载时

if(x==2||x==5) { val=DensityUtils.dp2px(GreetingsMakerActivity.this,val); }

这是布局文件以备不时之需

`<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/top_parent"><RelativeLayout
android:id="@+id/rl_parent"
android:layout_alignParentTop="true"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"><com.example.stickerview.sticker.StickerView
android:id="@+id/greetings_frame"
app:showBorder="false"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
app:showIcons="true"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/greetings_image_view"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.example.stickerview.sticker.StickerView>
</RelativeLayout>
</RelativeLayout>`

但是在不同的设备上结果仍然不同。它没有正确缩放和旋转。我究竟做错了什么?

标签: javaandroidmatrixscreen-densitydensity-independent-pixel

解决方案


推荐阅读