0

画像を回転させ、3 つの画像を重ねる必要があるという要件があります。ここに画像の説明を入力

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/rellay"
    tools:context=".MainActivity" >

  <com.example.stackableimages.ImageRotatedView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/square"
        android:id="@+id/image" />
    <com.example.stackableimages.ImageRotatedView2
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/square2"
        android:id="@+id/image1" />

</RelativeLayout>

注: でたらめに積み重ねる必要があります。つまり、毎回回転角度が異なります。回転アニメーションは解決策ではありません。3 つの imageviews で相対レイアウトを使用しました。カスタム ビューを作成して onDraw メソッドをオーバーライドする必要がありますか

View.setRotation はこれを実現するのに役立ちますが、 api 11 以降で利用できます。私のアプリは api 7 から互換性があるはずです。Androidでこれを達成するにはどうすればよいですか。上記のコードを使用すると、相対レイアウトを使用しているにもかかわらず、単一の画像しか表示できませんか?

4

1 に答える 1

2

カスタム ビュー:

public class MyImageView extends ImageView{

    private int mHeight;
    private int mWidth;
    private float mRotate;

    public MyImageView(Context context) {
        super(context);
        initRotate();
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRotate();
    }

    private void initRotate(){
        mRotate = (new Random().nextFloat() - 0.5f) * 30;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = bottom - top;
        mHeight = right - left;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int borderWidth = 2;
        canvas.save();
        canvas.rotate(mRotate, mWidth / 2, mHeight / 2);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(0xffffffff);
        canvas.drawRect(getPaddingLeft() - borderWidth, getPaddingTop() - borderWidth, mWidth - (getPaddingRight() - borderWidth), mHeight - (getPaddingBottom() - borderWidth), paint);
        super.onDraw(canvas);
        canvas.restore();
    }
}

そしてレイアウト:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
            />
    <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="30dp">

        <com.capitan.TestRotation.MyImageView
                android:id="@+id/image_view1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/your_drawable"
                android:padding="10dp"/>
        <com.capitan.TestRotation.MyImageView
                android:id="@+id/image_view2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/your_drawable"
                android:padding="10dp"/>
        <com.capitan.TestRotation.MyImageView
                android:id="@+id/image_view3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/your_drawable"
                android:padding="10dp"/>

    </FrameLayout>
</LinearLayout>

ImageViews にはパディングが必要です。それらを追加するのを忘れると、写真が切り取られます。

于 2013-05-16T10:29:24.230 に答える