欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中TextView和ImageView实现倾斜效果

程序员文章站 2023-12-04 10:25:52
textview倾斜: 想做一个倾斜的textview,想海报上显示的那样 ,在网上找例子一直不能实现,看了看textview源码,发现很简单,为方便像我一样糊涂的孩...

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> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。