Как повернуть нарисованное, но не само изображение? - PullRequest
3 голосов
/ 17 ноября 2011

У меня есть изображение с отрисовкой внутри. Этот рисунок является кольцом, похожим на то, которое присутствует в диалоговом окне прогресса:

loading_ring:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="3"
    android:thicknessRatio="8"
    android:useLevel="false">
    <size
        android:width="20dip"
        android:height="20dip"/>

    <gradient
        android:type="sweep"
        android:useLevel="false"
        android:startColor="#4c737373"
        android:centerColor="#4c737373"
        android:centerY="0.50"
        android:endColor="#ffffffff"/>
</shape>

Чтобы повернуть это кольцо, я создал анимацию вращения, подобную этой:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="359"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:duration="1000"
    android:interpolator="@android:anim/linear_interpolator" />

но когда я применяю анимацию к imageView:

this.cameraView.setAnimation(AnimationUtils.loadAnimation(this,R.anim.loadinganimation));

Весь imageView вращается. Я хотел бы повернуть только кольцо (источник изображения), как мне этого добиться?

Спасибо

Ответы [ 2 ]

2 голосов
/ 17 ноября 2011
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
       R.drawable.android);

int width = bitmapOrg.width();
int height = bitmapOrg.height();

// createa matrix for the manipulation
Matrix matrix = new Matrix();

// rotate the Bitmap
matrix.postRotate(45);

// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                  width, height, matrix, true);

// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);

Подробнее см.

http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

1 голос
/ 17 ноября 2011

Создайте два отдельных представления и используйте FrameLayout для их хранения. Таким образом, вы сможете применять анимацию к кольцу, а не к остальным виджетам.

EDIT:

Полагаю, вы помещаете фигуру внутри ImageView с помощью android:src и устанавливаете что-то еще с помощью android:background. Если это так, вместо ImageView вы можете попробовать:

<FrameLayout>
  <ImageView with the background>
  <ImageView with JUST the shape>
</FrameLayout>

или

<FrameLayout android:background="blah">
  <ImageView with JUST the shape>
</FrameLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...