标签:android应用
<span style="font-family:Times New Roman;font-size:18px;">package com.example.matrix; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.View; public class MyView extends View { private Bitmap bitmap; // 初始化图片资源 private int width, height; // 位图高、宽 private Matrix matrix = new Matrix(); // Matrix实例 private float sx = 0.0f; // 设置倾斜度 private float scale = 1.0f; // 设置缩放比例 private boolean isScale = false; //判断是缩放还是旋转 /* * 1.构造方法 功能:获得当前位图资源并且使得当前视图获得焦点 */ public MyView(Context context, AttributeSet attrs) { super(context, attrs); bitmap = ((BitmapDrawable) context.getResources().getDrawable(R.drawable.a)).getBitmap(); // 获得当前位图资源 width = bitmap.getWidth(); // 返回位图的宽度 height = bitmap.getHeight(); // 返回位图的高度 this.setFocusable(true); // 使当前视图获得焦点 } /* * 2.onDraw方法 功能:选择Matrix控制功能,创建新位图并绘制到View视图 */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // a.设置Matrix,选择Matrix控制功能 matrix.reset(); // 重置Matrix if (!isScale) // 设置Matrix为旋转或者缩放位图 { matrix.setSkew(sx, 0); // 控制Matrix进行旋转 } else { matrix.setScale(scale, scale);// 控制Matrix进行缩放 } // b.根据原始位图和Matrix创建新图片 Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); // c.View的canvas绘制新位图 canvas.drawBitmap(bitmap2, matrix, null); } /* 3.触屏事件 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { //向左倾斜 case KeyEvent.KEYCODE_A: isScale = false; sx +=0.1; postInvalidate(); //重绘View,即调用onDraw方法 break; //向右倾斜 case KeyEvent.KEYCODE_D: isScale = false; sx -=0.1; postInvalidate(); //重绘View,即调用onDraw方法 break; //放大 case KeyEvent.KEYCODE_W: isScale = true; if(scale<2.0) scale +=0.1; postInvalidate(); //重绘View,即调用onDraw方法 break; //缩小 case KeyEvent.KEYCODE_S: isScale = true; if(scale>0.5) scale -=0.1; postInvalidate(); //重绘View,即调用onDraw方法 break; } return super.onKeyDown(keyCode, event); } }</span>
<span style="font-family:Times New Roman;font-size:18px;">package com.example.drawbitmapmesh; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; public class WrapTest extends Activity { private Bitmap bitmap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this,R.drawable.photo)); } private class MyView extends View { //定义两个常量,这两个常量指定该图片横向,纵向上都被划分为20格 private final int WIDTH = 20; private final int HEIGHT = 20; //记录该图片上包含441个顶点 private final int COUNT = (WIDTH + 1) * (HEIGHT + 1); //定义一个数组,记录Bitmap上的21*21个点的坐标 private final float[] verts = new float[COUNT * 2]; //定义一个数组,记录Bitmap上的21*21个点经过扭曲后的坐标 //对图片扭曲的关键就是修改该数组里元素的值 private final float[] orig = new float[COUNT * 2]; /*1.构造方法*/ public MyView(Context context, int drawableId) { super(context); setFocusable(true); //使当前位图获取焦点 bitmap = BitmapFactory.decodeResource(getResources(), drawableId); //根据指定资源加载图片 float bitmapWidth = bitmap.getWidth(); //获取位图的宽度 float bitmapHeight = bitmap.getHeight(); //获取位图的高度 int index = 0; for(int y = 0; y <= HEIGHT; y++) { float fy = bitmapHeight * y / HEIGHT; for(int x = 0 ; x <= WIDTH; x++) { float fx = bitmapWidth * x / WIDTH; //初始化orig,verts数组 //初始化,orig,verts两个数组均匀地保存了21 * 21个点的x,y坐标 orig[index * 2 + 0] = verts[index * 2 + 0] = fx; orig[index * 2 + 1] = verts[index * 2 + 1] = fy; index += 1; } } setBackgroundColor(Color.WHITE); //设置背景颜色 } /*2.绘图方法 * 功能:绘制图形。对bitmap按verts数组进行扭曲 * 从第一个点(由第5个参数0控制)开始扭曲*/ @Override protected void onDraw(Canvas canvas) { canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts, 0, null, 0, null); } /*3.工具方法 * 功能:用于根据触摸事件的位置计算verts数组里各元素的值*/ private void warp(float cx,float cy) { for(int i=0;i<COUNT*2;i+=2) { float dx = cx - orig[i+0]; float dy = cy - orig[i+1]; float dd = dx*dx+dy*dy; //计算每个坐标点与当前(cx,cy)之间的距离 float d = (float)Math.sqrt(dd); //计算扭曲度,距离当前点(cx,cy)越远,扭曲度越小 float pull = 80000 / ((float)(dd*d)); //对verts数组(保存bitmap上21*21个点经过扭曲后的坐标)重新赋值 if(pull>=1) { verts[i+0]=cx; verts[i+1]=cy; } else { //控制各顶点向触摸事件发生点偏移 verts[i+0] = orig[i+0]+dx*pull; verts[i+1] = orig[i+1]+dy*pull; } invalidate(); //通知View组件重绘 } } /*4.触摸屏监听器 * 功能:触碰事件响应-调用warp方法根据触摸屏事件的坐标来扭曲verts数组*/ @Override public boolean onTouchEvent(MotionEvent event) { warp(event.getX(),event.getY()); return true; } } }</span>
<span style="font-family:Times New Roman;font-size:18px;">package com.example.shader; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint= new Paint() ; public MyView(Context context, AttributeSet attrs) { super(context, attrs); paint= new Paint(); paint.setAntiAlias(true); // 去锯齿 paint.setStyle(Paint.Style.FILL); // 设置画笔的填充风格 } @Override protected void onDraw(Canvas canvas) { canvas.drawRect(30, 30, 200,200, paint); } }</span>
<span style="font-family:Times New Roman;font-size:18px;">package com.example.shader; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapShader; import android.graphics.Color; import android.graphics.ComposeShader; import android.graphics.LinearGradient; import android.graphics.PorterDuff; import android.graphics.RadialGradient; import android.graphics.Shader; import android.graphics.Shader.TileMode; import android.graphics.SweepGradient; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ShaderTest extends Activity implements OnClickListener { private Shader[] shaders = new Shader[5]; //声明位图渲染对象 private int[] colors; //声明颜色数组 MyView myView; //自定义视图类 /*1.初始化*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myView = (MyView)findViewById(R.id.my_view); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.water); //获取Bitmap实例 colors = new int[] {Color.RED,Color.GREEN,Color.BLUE}; //设置渐变的颜色组 shaders[0] = new BitmapShader(bitmap, TileMode.REPEAT, TileMode.MIRROR);//实例话BitmapShader,x坐标方法重复图形,y坐标方向镜像图形 shaders[1] =new LinearGradient(0, 0, 100, 100, colors, null, TileMode.REPEAT); //实例化LinearGradient shaders[2]=new RadialGradient(100, 100, 80, colors, null, TileMode.REPEAT); //实例化RadialGradient shaders[3]=new SweepGradient(160, 160, colors, null); //实例化SweepGradient shaders[4]=new ComposeShader(shaders[1], shaders[2], PorterDuff.Mode.DARKEN);//实例化ComposeShader //获取界面按钮并为其注册事件监听器 Button btn1 = (Button)findViewById(R.id.bn1); Button btn2 = (Button)findViewById(R.id.bn2); Button btn3 = (Button)findViewById(R.id.bn3); Button btn4 = (Button)findViewById(R.id.bn4); Button btn5 = (Button)findViewById(R.id.bn5); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); } @Override public void onClick(View source) { switch(source.getId()) { case R.id.bn1: myView.paint.setShader(shaders[0]); break; case R.id.bn2: myView.paint.setShader(shaders[1]); break; case R.id.bn3: myView.paint.setShader(shaders[2]); break; case R.id.bn4: myView.paint.setShader(shaders[3]); break; case R.id.bn5: myView.paint.setShader(shaders[4]); break; } //重绘界面 myView.invalidate(); } }</span>
<span style="font-family:Times New Roman;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 5个Button按钮(略) --> <!-- 位图 --> <com.example.shader.MyView android:id="@+id/my_view" android:layout_width="match_parent" android:layout_height="251dp" /> </LinearLayout></span>
标签:android应用
原文地址:http://blog.csdn.net/u012637501/article/details/43484633