gpt4 book ai didi

android - 使用android API = 8在背景图像上旋转透明png图像

转载 作者:行者123 更新时间:2023-11-30 03:09:46 24 4
gpt4 key购买 nike

我有两张图片。背景是这样的 enter image description here另一个是这个 enter image description here第二个有透明背景。两者都是PNG图像。我有一个背景上有箭头的布局,它看起来很完美,就像这样 enter image description here

我想旋转箭头指向风吹的方向。我有一个循环,每秒运行一次并更新每个循环的角度。

这是我的布局:

        <FrameLayout
android:id="@+id/frameMasthead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonReturn"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/Button01" >

<ImageView
android:id="@+id/imageMasthead"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@raw/masthead"
/>
<ImageView
android:id="@+id/imageMastheadFly"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@raw/masthead_fly"
/>

这是我在搜索 Google 和 SO 后尝试过的代码。

imageMastheadFly = (ImageView)findViewById(R.id.imageMastheadFly);
Matrix matrix=new Matrix();
imageMastheadFly.setScaleType(ScaleType.MATRIX); //required
matrix.postRotate((float) awa, imageMastheadFly.getDrawable().getBounds().width()/2, imageMastheadFly.getDrawable().getBounds().height()/2);
imageMastheadFly.setImageMatrix(matrix);

它编译并运行,但结果不是我想要的。我时不时地注意到指针在框架之外经过,并且是全尺寸,而不是缩小的框架尺寸。这是我看到的示例:

enter image description here您可以看到背景很好,但箭头是全尺寸的并且超出了框架。

我找到的一些答案不起作用,因为这是一个 API=8 的应用程序,因此它可以在我的所有手机上运行。

如果我放弃 api=8 要求并将代码更改为:

imageMastheadFly = (ImageView)findViewById(R.id.imageMastheadFly);
imageMastheadFly.setRotation((float) awa);

它工作正常。它只是不适用于像我的 Nexus-One 这样的旧手机

我还必须将其添加到 XML

android:rotation="0"

答案 #1 建议我按照教程进行操作,我照做了。这是我运行的代码:

imageMastheadFly = (ImageView)findViewById(R.id.imageMastheadFly);
Matrix matrix=new Matrix();
Bitmap bMap;
bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
matrix.postRotate((float) awa);
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
bMap.getWidth(), bMap.getHeight(), matrix, true);
imageMastheadFly.setImageBitmap(bMapRotate);

发生的事情是没有使用箭头图像,而是我的应用程序的图标位于背景之上。当它旋转时,它不仅会旋转,还会自行缩放,以便即使旋转 45 度也能适应框架布局。这不是我想要的。我希望箭头在不缩放的情况下旋转,所以看起来风只是在旋转箭头。教程中的这段代码看起来不对。似乎缺少旋转图像的规范,并且确实无法正常工作。

我能够让 RotateAnimation 在 API=8 下工作,所以问题就解决了。这是代码。

imageMastheadFly = (ImageView)findViewById(R.id.imageMastheadFly);
RotateAnimation r; // = new RotateAnimation(ROTATE_FROM, ROTATE_TO);
r = new RotateAnimation((float)oldAwa, (float)awa, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
r.setDuration((long) 1000);
r.setRepeatCount(0);
imageMastheadFly.startAnimation(r);
oldAwa = awa;

最佳答案

使用 ImageView 的矩阵旋转它,您的问题可能是由于旋转原点设置错误引起的 - 可能与使用可绘制对象的边界而不是 View 的大小有关。

不要每次都创建位图。

或者,您可以在 View 上使用 RotateAnimation,其中 fillEnabled、fillAfter、fillBefore 都设置为 true。

关于android - 使用android API = 8在背景图像上旋转透明png图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21176662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com