您好,登錄后才能下訂單哦!
TextView傾斜:
想做一個傾斜的TextView,想海報上顯示的那樣 ,在網上找例子一直不能實現,看了看TextView源碼,發現很簡單,為方便像我一樣糊涂的孩紙,貼出來了.
首先需要先自定義一個TextView
public class MyTextView extends TextView{ public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { //傾斜度45,上下左右居中 canvas.rotate(-45, getMeasuredWidth()/2, getMeasuredHeight()/2); super.onDraw(canvas); } }
xml文件中添加上這個MyTextView
<com.peace.test.incline_textview.MyTextView android:layout_width="100dip" android:layout_height="100dip" android:gravity="center" android:text="@string/hello_world"/>
ImageView傾斜:
MainActivity如下:
import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.app.Activity; import android.graphics.Matrix; /** * Demo描述: * 利用mImageView.setImageMatrix(matrix)實現 * 圖片的平移,縮放,旋轉,傾斜以及對稱 * * 參考資料: * 0 http://blog.csdn.net/pathuang68/article/details/6991988 * 1 http://blog.csdn.net/mingli198611/article/details/7830633 * * Thank you very much */ public class MainActivity extends Activity { private TestMatrixImageView mTestMatrixImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTestMatrixImageView=new TestMatrixImageView(MainActivity.this); mTestMatrixImageView.setScaleType(ImageView.ScaleType.MATRIX);//?? mTestMatrixImageView.setOnTouchListener(new TouchListenerImpl()); setContentView(mTestMatrixImageView); } private class TouchListenerImpl implements OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_UP) { //1 測試平移 testTranslate(); //2 測試圍繞圖片中心點旋轉 //testRotate(); //3 測試圍繞原點旋轉后平移 //testRotateAndTranslate(); //4 縮放 //testScale(); //5 水平傾斜 //testSkewX(); //6 垂直傾斜 //testSkewY(); //7 水平且垂直傾斜 //testSkewXY(); //8 水平對稱 //testSymmetryX(); //9 垂直對稱 //testSymmetryY(); //10 關于X=Y對稱 //testSymmetryXY(); } return true; } } //平移 private void testTranslate(){ Matrix matrix=new Matrix(); int width=mTestMatrixImageView.getBitmap().getWidth(); int height=mTestMatrixImageView.getBitmap().getHeight(); matrix.postTranslate(width, height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //圍繞圖片中心點旋轉 private void testRotate(){ Matrix matrix=new Matrix(); int width=mTestMatrixImageView.getBitmap().getWidth(); int height=mTestMatrixImageView.getBitmap().getHeight(); matrix.postRotate(45f, width/2, height/2); matrix.postTranslate(width, height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //圍繞原點旋轉后平移 //注意以下三行代碼的執行順序: //matrix.setRotate(45f); //matrix.preTranslate(-width, -height); //matrix.postTranslate(width, height); //先執行matrix.preTranslate(-width, -height); //后執行matrix.setRotate(45f); //再執行matrix.postTranslate(width, height); private void testRotateAndTranslate() { Matrix matrix = new Matrix(); int width = mTestMatrixImageView.getBitmap().getWidth(); int height = mTestMatrixImageView.getBitmap().getHeight(); matrix.setRotate(45f); matrix.preTranslate(-width, -height); matrix.postTranslate(width, height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //縮放 private void testScale() { Matrix matrix = new Matrix(); matrix.setScale(0.5f, 0.5f); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //水平傾斜 private void testSkewX() { Matrix matrix = new Matrix(); matrix.setSkew(0.5f, 0); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 垂直傾斜 private void testSkewY() { Matrix matrix = new Matrix(); matrix.setSkew(0, 0.5f); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 水平且垂直傾斜 private void testSkewXY() { Matrix matrix = new Matrix(); matrix.setSkew(0.5f, 0.5f); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 水平對稱--圖片關于X軸對稱 private void testSymmetryX() { Matrix matrix = new Matrix(); int height = mTestMatrixImageView.getBitmap().getHeight(); float matrixValues[] = { 1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f }; matrix.setValues(matrixValues); //若是matrix.postTranslate(0, height); //表示將圖片上下倒置 matrix.postTranslate(0, height*2); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 垂直對稱--圖片關于Y軸對稱 private void testSymmetryY() { Matrix matrix = new Matrix(); int width=mTestMatrixImageView.getBitmap().getWidth(); float matrixValues[] = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f}; matrix.setValues(matrixValues); //若是matrix.postTranslate(width,0); //表示將圖片左右倒置 matrix.postTranslate(width*2, 0); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } // 關于X=Y對稱--圖片關于X=Y軸對稱 private void testSymmetryXY() { Matrix matrix = new Matrix(); int width = mTestMatrixImageView.getBitmap().getWidth(); int height = mTestMatrixImageView.getBitmap().getHeight(); float matrixValues[] = { 0f, -1f, 0f, -1f, 0f, 0f, 0f, 0f, 1f }; matrix.setValues(matrixValues); matrix.postTranslate(width+height, width+height); mTestMatrixImageView.setImageMatrix(matrix); showMatrixEveryValue(matrix); } //獲取變換矩陣Matrix中的每個值 private void showMatrixEveryValue(Matrix matrix){ float matrixValues []=new float[9]; matrix.getValues(matrixValues); for (int i = 0; i <3; i++) { String valueString=""; for (int j = 0; j < 3; j++) { valueString=matrixValues[3*i+j]+""; System.out.println("第"+(i+1)+"行的第"+(j+1)+"列的值為"+valueString); } } } }
TestMatrixImageView如下:
package cn.testmatrix; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.widget.ImageView; /** * 總結: * 在MainActivity中執行: * mTestMatrixImageView.setImageMatrix(matrix); * 時此自定義View會先調用setImageMatrix(Matrix matrix) * 然后調用onDraw(Canvas canvas) */ public class TestMatrixImageView extends ImageView{ private Matrix mMatrix; private Bitmap mBitmap; public TestMatrixImageView(Context context) { super(context); mMatrix=new Matrix(); mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon); } @Override protected void onDraw(Canvas canvas) { System.out.println("---> onDraw"); //畫原圖 canvas.drawBitmap(mBitmap, 0, 0, null); //畫經過Matrix變化后的圖 canvas.drawBitmap(mBitmap, mMatrix, null); super.onDraw(canvas); } @Override public void setImageMatrix(Matrix matrix) { System.out.println("---> setImageMatrix"); this.mMatrix.set(matrix); super.setImageMatrix(matrix); } public Bitmap getBitmap(){ System.out.println("---> getBitmap"); return mBitmap; } }
main.xml如下:
<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。