Custom view:
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();
}
}
And layout:
<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>
Paddings are required in ImageViews. If you forget to add them, your pictures would be clipped.