SoFunction
Updated on 2025-04-07

TextView and ImageView achieve tilt effect in Android

TextView tilt:

I want to make a tilted TextView, and I want to show it on the poster. I have never realized it when I look at the TextView source code and found it very simple. I posted it for the convenience of a child as confused as me.

First, you need to customize a TextView first

public class MyTextView extends TextView{ 
 
  public MyTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
  } 
 
  @Override 
  protected void onDraw(Canvas canvas) { 
    //Inclination is 45, up and down, left and right center    (-45, getMeasuredWidth()/2, getMeasuredHeight()/2); 
    (canvas); 
  } 
   
} 

Add this MyTextView to the xml file

<.incline_textview.MyTextView 
    android:layout_width="100dip" 
    android:layout_height="100dip" 
    android:gravity="center" 
    android:text="@string/hello_world"/> 

ImageView tilt:

MainActivity is as follows:

import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
/**
  * Demo description:
  * Using (matrix) to implement
  * Translation, zoom, rotation, tilt and symmetry of the picture
  *
  * References:
  * 0 /pathuang68/article/details/6991988
  * 1 /mingli198611/article/details/7830633
  *
  * Thank you very much
  */ 
public class MainActivity extends Activity { 
 private TestMatrixImageView mTestMatrixImageView; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  mTestMatrixImageView=new TestMatrixImageView(); 
  ();//?? 
  (new TouchListenerImpl()); 
  setContentView(mTestMatrixImageView); 
 } 
  
 private class TouchListenerImpl implements OnTouchListener{ 
  @Override 
  public boolean onTouch(View v, MotionEvent event) { 
   if (()==MotionEvent.ACTION_UP) { 
    //1 Test translation    testTranslate(); 
    //2 Test rotates around the center point of the picture    //testRotate(); 
    //3 Test to rotate around the origin and then translate    //testRotateAndTranslate(); 
    //4 Zoom    //testScale(); 
    //5 Horizontal tilt    //testSkewX(); 
    //6 Vertical tilt    //testSkewY(); 
    //7 Horizontal and vertical incline    //testSkewXY(); 
    //8 Horizontal symmetry    //testSymmetryX(); 
    //9 Vertical symmetry    //testSymmetryY(); 
    //10 About X=Y symmetry    //testSymmetryXY(); 
   } 
   return true; 
  } 
   
 } 
  
 //Platform private void testTranslate(){ 
  Matrix matrix=new Matrix(); 
  int width=().getWidth(); 
  int height=().getHeight(); 
  (width, height); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 } 
 //Rolleate around the center point of the picture private void testRotate(){ 
  Matrix matrix=new Matrix(); 
  int width=().getWidth(); 
  int height=().getHeight(); 
  (45f, width/2, height/2); 
  (width, height); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 } 
  
 //Play around the origin //Note the execution order of the following three lines of code: //(45f); 
 //(-width, -height); 
 //(width, height); 
 //Execute (-width, -height); //Execute afterwards (45f); //Execute again (width, height); private void testRotateAndTranslate() { 
  Matrix matrix = new Matrix(); 
  int width = ().getWidth(); 
  int height = ().getHeight(); 
  (45f); 
  (-width, -height); 
  (width, height); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 } 
  
 //Zoom private void testScale() { 
  Matrix matrix = new Matrix(); 
  (0.5f, 0.5f); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 } 
  
 //Horizontal tilt private void testSkewX() { 
  Matrix matrix = new Matrix(); 
  (0.5f, 0); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 } 
 
 // Vertical tilt private void testSkewY() { 
  Matrix matrix = new Matrix(); 
  (0, 0.5f); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 } 
  
 // Incline horizontally and vertically private void testSkewXY() { 
  Matrix matrix = new Matrix(); 
  (0.5f, 0.5f); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 } 
  
 // Horizontal Symmetry--Picture about X-axis symmetry private void testSymmetryX() { 
  Matrix matrix = new Matrix(); 
  int height = ().getHeight(); 
  float matrixValues[] = { 1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f }; 
  (matrixValues); 
  //If it is (0, height);  // means to put the picture up and down  (0, height*2); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 } 
  
 // Vertical symmetry--Picture about Y-axis symmetry private void testSymmetryY() { 
  Matrix matrix = new Matrix(); 
  int width=().getWidth(); 
  float matrixValues[] = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f}; 
  (matrixValues); 
  //If so (width, 0);  // means to turn the picture left and right invert  (width*2, 0); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 
 } 
 
 // About X=Y symmetry--Picture About X=Y Axis symmetry private void testSymmetryXY() { 
  Matrix matrix = new Matrix(); 
  int width = ().getWidth(); 
  int height = ().getHeight(); 
  float matrixValues[] = { 0f, -1f, 0f, -1f, 0f, 0f, 0f, 0f, 1f }; 
  (matrixValues); 
  (width+height, width+height); 
  (matrix); 
  showMatrixEveryValue(matrix); 
 } 
  
 //Get each value in the transformation matrix Matrix private void showMatrixEveryValue(Matrix matrix){ 
  float matrixValues []=new float[9]; 
  (matrixValues); 
  for (int i = 0; i &lt;3; i++) { 
   String valueString=""; 
   for (int j = 0; j &lt; 3; j++) { 
    valueString=matrixValues[3*i+j]+""; 
    ("Third"+(i+1)+"Original"+(j+1)+"The value of the column is"+valueString); 
   } 
  } 
 } 
 
} 

The TestMatrixImageView is as follows:

package ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
/**
  * Summarize: 
  * Execute in MainActivity:
  * (matrix);
  * At this custom view, setImageMatrix (Matrix matrix) will be called first.
  * Then call onDraw(Canvas canvas)
  */ 
public class TestMatrixImageView extends ImageView{ 
 private Matrix mMatrix; 
 private Bitmap mBitmap; 
 public TestMatrixImageView(Context context) { 
  super(context); 
  mMatrix=new Matrix(); 
  mBitmap=(getResources(), ); 
 } 
 @Override 
 protected void onDraw(Canvas canvas) { 
  ("---&gt; onDraw"); 
  //Draw the original picture  (mBitmap, 0, 0, null); 
  //Draw pictures after Matrix changes  (mBitmap, mMatrix, null); 
  (canvas); 
 } 
 @Override 
 public void setImageMatrix(Matrix matrix) { 
  ("---&gt; setImageMatrix"); 
  (matrix); 
  (matrix); 
 } 
  
 public Bitmap getBitmap(){ 
  ("---&gt; getBitmap"); 
  return mBitmap; 
 } 
 
} 

as follows:

<RelativeLayout xmlns:andro 
 xmlns:tools="/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 > 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="@string/hello_world" /> 
 
</RelativeLayout> 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.