Search code examples
androidandroid-custom-view

How to tilt a child layout at some angle


I've tried the custom view and tilt the image to 45 degree successfully but I want to tilt the whole child layoout at some angle so that whatever I'll add in that child layout will automactically tiled .. For reference please take a look at the attached picture enter image description here

even I have tried this code but it wont help

public class CustomLinear extends LinearLayout{
private Matrix mForward = new Matrix();
private Matrix mReverse = new Matrix();
private float[] mTemp = new float[2];

public CustomLinear(Context context) {
super(context);
}

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

@Override
protected void dispatchDraw(Canvas canvas) {

canvas.rotate(75, getWidth() / 2, getHeight() / 2);

mForward = canvas.getMatrix();
mForward.invert(mReverse);
canvas.save();
canvas.setMatrix(mForward); // This is the matrix we need to use for
                                        // proper positioning of touch events
super.dispatchDraw(canvas);
canvas.restore();
invalidate();
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
event.setLocation(getWidth() - event.getX(), getHeight() - event.getY());
return super.dispatchTouchEvent(event);
}

}

Solution

  • I thing it is possible using rotate animation. you can apply animation on the whole child layout with 0 duration in On_create.

    Here is the animation code

    <?xml version="1.0" encoding="utf-8"?>
        <rotate
         xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromDegrees="0"
            android:toDegrees="-90"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="0"
            android:fillAfter="true">
        </rotate>
    

    You can set the rotation angle as per your need.

    Then use given code in your Activity's onCreate,

      @Override
      public void onCreate(Bundle b) {
      super.onCreate(b);
         setContentView(R.layout.myscreen);
         Animation rotate_anim = AnimationUtils.loadAnimation(this, R.anim.rotation);
         LayoutAnimationController anim_controller = new LayoutAnimationController(rotate_anim, 0);
         FrameLayout layout = (FrameLayout)findViewById(R.id.MyScreen_ContentLayout);
         layout.setLayoutAnimation(anim_controller);
     }