ShapeDrawable放大镜的案例

[MickleZcg]

 

http://bbs.ldci.com.cn/simple/?t3094.html

 

ShapeDrawable放大镜的案例

放大镜的案例

如果要在圆形区域放大,一种解决的办法是切出圆形的图在放大。一种是最好能够有设定任意形状的可显示区域,找了老半天也没找到。

觉得要动用C的CGI技术的时候,我想到了ShapeDrawable,ShapeDrawable是可以设置画的形状的。但不知道他有没有可以设置要画的像素组,查了一下,真是惊喜万分啊。shape.getPaint().setShader()可以设置    Shader,Shader可以接受一个图像的数据,通过BitmapShader(Bitmap, Shader.TileMode.REPEAT,
                                      Shader.TileMode.REPEAT);可以得到Shader,通过这两个方法我实现了在圆形区域显示图像的要求
现在贡献我的代码

  1. package com.ui;
  2. import android.view.KeyEvent;
  3. import android.view.MotionEvent;
  4. import android.view.View;
  5. import android.view.View;
  6. import android.graphics.Matrix;
  7. import android.content.Context;
  8. import android.graphics.Canvas;
  9. import android.graphics.Bitmap;
  10. import android.graphics.Paint;
  11. import android.graphics.Paint.Style;
  12. import android.graphics.Region;
  13. import android.graphics.Rect;
  14. import com.util.Tools;
  15. import android.graphics.drawable.PaintDrawable;
  16. import android.util.Log;  
  17. import  com.main.main;
  18. import com.main.R;
  19. import android.graphics.MaskFilter;
  20. import android.graphics.EmbossMaskFilter;
  21. import android.graphics.Path;
  22. import android.graphics.BlurMaskFilter;
  23. import android.graphics.BitmapFactory;
  24. import android.graphics.RectF;
  25. import android.graphics.drawable.ShapeDrawable;
  26. import android.graphics.drawable.ShapeDrawable.ShaderFactory;
  27. import android.graphics.drawable.shapes.Shape;
  28. import android.graphics.drawable.shapes.OvalShape;
  29. import android.graphics.BitmapShader;
  30. import android.graphics.Shader;
  31. public class FaceView extends View
  32.       
  33. {
  34.    private float rectx=20;
  35.    private float recty=20;
  36.    private Bitmap bmp[]=new Bitmap[1];
  37.    private Bitmap fp=null;
  38.    private Bitmap bmp2=null;
  39.    private Bitmap tmp[]=new Bitmap[1];
  40.    private EmbossMaskFilter filter=null;
  41.    protected static final String ACTIVITY_TAG="FaceView";  
  42.    private float fangda=1;
  43.    private float dushu[]=new float[]{0,30,60,90};
  44.    private int index=0;
  45.    private int peishu=1;
  46.    private boolean isfandasuoxiao=true;
  47.    private main main=null;
  48. public void setMain(main main) {
  49.   this.main = main;
  50.   //this.getContext().
  51. }
  52. //  private Matrix rx=null;
  53.    Paint p;
  54.   // private ShapeDrawable drawable =null;
  55.    public FaceView(android.content.Context context){
  56.     super(context);
  57.    // context.c
  58.   //  this.c.
  59.   //  context.deleteDatabase(arg0)
  60.     //context.openOrCreateDatabase(arg0, arg1, arg2)
  61.     p=new Paint();
  62.     p.setColor(-1);
  63.    // context.
  64.     fp=BitmapFactory.decodeResource( this.getResources(), R.drawable.fd);
  65.     //p.setMaskFilter(maskfilter)
  66.   //  p.
  67.     //rx = new Matrix();
  68.   //  setLongClickable(true);
  69.   // this.setOnTouchListener(this);
  70.    
  71.    // RectShape r=  new RectShape() ;
  72.    
  73.   //=new ShapeDrawable(new RectShape() );
  74.   //.getPaint().setColor(-1);
  75.    
  76.    }
  77.    public  Bitmap makeDst(int w, int h) {
  78.    Bitmap bmy =this.bmp2;
  79.          
  80.          Paint py = new Paint(Paint.ANTI_ALIAS_FLAG);
  81.          Canvas c = new Canvas(bmy);
  82.          py.setColor(-1);
  83.          py.setStyle(Style.STROKE);
  84.          py.setAlpha(50);
  85.          c.drawOval(new RectF(0, 0, this.bmp2.getWidth()*3/4, this.bmp2.getHeight()*3/4), py);
  86.          c.setBitmap(bmy);
  87.          return bmy;
  88.      }
  89.    private  Shader makeTiling() {
  90.          
  91.          
  92.           return new BitmapShader(this.bmp2, Shader.TileMode.REPEAT,
  93.                                       Shader.TileMode.REPEAT);
  94.       }
  95.    public ShapeDrawable drawbmp(){
  96.   
  97.     ShapeDrawable shape=new ShapeDrawable(new OvalShape());
  98.     shape.getPaint().setShader(makeTiling());
  99.     return shape;
  100.    
  101.    }
  102.    public void setBitmap(Bitmap bmp) {
  103.    this.bmp[0]=bmp;
  104.    System.arraycopy(this.bmp,0 ,this.tmp , 0, 1);
  105.    fangda=1;
  106.    // dushu=0;
  107.     peishu=1;
  108.    // chang_Bmp();
  109.    }
  110. @Override
  111. protected void onDraw(Canvas canvas) {
  112.   // TODO Auto-generated method stub
  113.   
  114.   p.setStyle(Style.STROKE);
  115.   //p.
  116.   //.d
  117.   if(this.bmp[0]!=null){
  118.   canvas.drawBitmap(this.bmp[0], 0, 0, new Paint());
  119.   Rect  oldrect=canvas.getClipBounds();
  120.   //canvas.clipRegion(region)
  121.   canvas.drawText("放大", 2, this.getHeight()-30, p);
  122.   canvas.drawText("缩小", 80, this.getHeight()-30, p);
  123.   canvas.drawText("旋转", 150, this.getHeight()-30, p);
  124.   canvas.drawText("退出", this.getWidth()-40, this.getHeight()-30, p);
  125.   //canvas.
  126.   
  127.   if(bmp2!=null){
  128.   
  129.   // canvas.clipRect(rectx, recty, rectx+180, recty+180, Op.DIFFERENCE);
  130.    //canvas.
  131.    //canvas.d
  132.    //canvas.clipRegion(region)
  133.    
  134.    //this.bmp2.extractAlpha(pt, new int[]{(int)rectx+50,(int)recty+50});
  135.    //canvas.drawBitmap(drawbmp(), rectx+8, recty+8, new Paint());
  136.    ShapeDrawable sha= drawbmp();
  137.    sha.setBounds((int)rectx, (int)recty, (int)rectx+fp.getWidth(), (int)recty+fp.getWidth());
  138.    sha.draw(canvas);
  139.    //canvas.drawVertices(mode, vertexCount, verts, vertOffset, texs, texOffset, colors, colorOffset, indices, indexOffset, indexCount, paint)
  140.    //.drawBitmap(this.bmp2, rectx, recty, new Paint());
  141.    //canvas.drawBitmapMesh(bitmap, meshWidth, meshHeight, verts, vertOffset, colors, colorOffset, paint)
  142.    
  143.   }
  144.   float[] tempArr={200,10,100};
  145.   Paint tp=new Paint();
  146.   tp.setStyle(Style.STROKE);
  147.   tp.setColor(-1);
  148.   tp.setAlpha(100);
  149.   EmbossMaskFilter emm =new EmbossMaskFilter(tempArr,0.5f,100,10.5f);
  150.   tp.setMaskFilter(emm);
  151. // tp.
  152.   
  153.   //canvas.drawRect(rectx, , (rectx+180)*3/4, (recty+180)*3/4, tp);
  154.   canvas.drawCircle(rectx+fp.getWidth()/2,  recty+fp.getWidth()/2, fp.getWidth()/2,tp);
  155.   canvas.drawBitmap(this.fp, rectx, recty, new Paint());
  156.   //canvas.c
  157.   //canvas.drawBitmapMesh(bitmap, meshWidth, meshHeight, verts, vertOffset, colors, colorOffset, paint)
  158. // canvas.drawBitmap(makeDst(50,50), 10,10,new  Paint());
  159.   //canvas.drawBitmap(fp, rectx, recty, new Paint());
  160.   //canvas.drawRect(rectx, recty, rectx+180, recty+180, p);
  161.   //canvas.drawRect(rectx+2, recty+2, rectx+180-2, recty+180-2, p);
  162. // canvas.clipRect(oldrect);
  163.   }
  164.   
  165.   //canvas.
  166.   //drawable.setBounds(20, 20, 250, 250);
  167.   //drawable.draw(canvas);
  168.   //p.
  169.   
  170. }
  171. @Override
  172. public boolean onKeyDown(int keyCode, KeyEvent event) {
  173.   // TODO Auto-generated method stub
  174.   return super.onKeyDown(keyCode, event);
  175. }
  176. @Override
  177. public boolean onKeyUp(int keyCode, KeyEvent event) {
  178.   // TODO Auto-generated method stub
  179.   return super.onKeyUp(keyCode, event);
  180. }
  181. @Override
  182. public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
  183.   // TODO Auto-generated method stub
  184.   return super.onKeyMultiple(keyCode, repeatCount, event);
  185. }
  186. @Override
  187. public boolean onTouchEvent(MotionEvent event) {
  188.   switch(event.getAction()){
  189.   case MotionEvent.ACTION_DOWN:
  190.    if( event.getY()<this.getHeight()-40 ){
  191.    
  192.    
  193.    this.rectx=event.getX();
  194.    this.recty=event.getY()-15;
  195.    if(this.rectx+180>=this.bmp[0].getWidth()){
  196.     this.rectx= this.bmp[0].getWidth()-180;
  197.    }
  198.    if(this.recty+180>=this.bmp[0].getHeight()){
  199.     this.recty=this.bmp[0].getHeight()-180;
  200.    }
  201.    chang_Bmp();
  202.    //this.bmp2=Tools.effect_cut(bmp3, ((int)this.rectx* (int)this.fangda+2), ((int)this.recty* (int)this.fangda+2), 100-2, 100-2);
  203.    Log.i(FaceView.ACTIVITY_TAG, "msg"+this.rectx);}else{
  204.     if(event.getRawX()>=2 && event.getRawX()<=40 && event.getRawY()<this.getHeight() && event.getRawY()>=this.getHeight()-40 ){
  205.      isfandasuoxiao=true;
  206.      if(this.fangda>=1){
  207.      this.peishu++;
  208.      if(this.peishu>=5){
  209.       this.peishu=5;
  210.      }
  211.      this.fangda=1*(float)this.peishu;}else{
  212.       this.peishu--;
  213.       if(this.peishu<=1){
  214.        this.peishu=1;
  215.       }
  216.       this.fangda=1/(float)this.peishu;
  217.      }
  218.      chang_Bmp();
  219.     }else if(event.getRawX()>=80 && event.getRawX()<=80+40 && event.getRawY()<this.getHeight() && event.getRawY()>=this.getHeight()-40){
  220.      isfandasuoxiao=false;
  221.      if(this.fangda>1){
  222.       this.peishu--;
  223.       if(this.peishu<=1){
  224.        this.peishu=1;
  225.       }
  226.       this.fangda=1*(float)this.peishu;}
  227.       chang_Bmp();
  228.     }else if(event.getRawX()>=150 && event.getRawX()<=150+40 && event.getRawY()<this.getHeight() && event.getRawY()>=this.getHeight()-40){
  229.      index++;
  230.      Matrix rx = new Matrix();
  231.      this.bmp2=null;
  232.      if(this.index>this.dushu.length-1){
  233.       this.index=0;
  234.       System.arraycopy(this.tmp,0 ,this.bmp , 0, 1);
  235.      }else{
  236.       
  237.      rx.postRotate(this.dushu[this.index]);
  238.      bmp[0]=Bitmap.createBitmap(bmp[0], 0, 0, bmp[0].getWidth(), bmp[0].getHeight(), rx, true);
  239.      }
  240.      //rx.p
  241.      //Bitmap bmp3=Tools.effect_cut(bmp1[0], ((int)this.rectx+2), ((int)this.recty+2), 100-2, 100-2);//切图片ue
  242.      
  243.     }else if(event.getRawX()>=this.getWidth()-40 && event.getRawX()<=this.getWidth() && event.getRawY()<this.getHeight() && event.getRawY()>=this.getHeight()-40){
  244.      this.main.finish();
  245.     }
  246.    
  247.    
  248.    }
  249.    
  250.    this.postInvalidate();
  251.    break;
  252.   }
  253.   
  254.   return super.onTouchEvent(event);
  255. }
  256. public void chang_Bmp(){
  257.   Bitmap bmp1[]=new Bitmap[1];
  258.   System.arraycopy(bmp,0 ,bmp1 , 0, 1);
  259.   if(this.fangda>0){
  260.   Matrix rx = new Matrix();
  261.   rx.postScale(this.fangda, this.fangda);
  262.   //rx.postSkew(0, 10, 5, 6);
  263.   //Bitmap bmp3=Tools.effect_cut(bmp1[0], ((int)this.rectx+2), ((int)this.recty+2), 100-2, 100-2);//切图片ue
  264.   
  265.   Bitmap bmp3=Bitmap.createBitmap(bmp1[0], 0, 0, bmp1[0].getWidth(), bmp1[0].getHeight(),rx,true);
  266.   //Bitmap.
  267.   this.bmp2=Bitmap.createBitmap(bmp3, (int)this.rectx*this.peishu, (int)this.recty*this.peishu, fp.getWidth(), fp.getWidth());
  268.   //this.bmp2=Tools.effect_hoodwink(this.bmp2);
  269.   }else{
  270.    Bitmap bmp3=Tools.effect_resizeImage(bmp1[0], bmp1[0].getWidth()/this.peishu, bmp1[0].getHeight()/this.peishu);
  271.    this.bmp2=Bitmap.createBitmap(bmp3, ((int)this.rectx/ (int)this.peishu+2), ((int)this.recty/(int)this.peishu+2), fp.getWidth(), fp.getWidth());
  272.    this.bmp2=Tools.effect_hoodwink(this.bmp2);
  273.   
  274.   }
  275. }
  276. @Override
  277. public boolean onKeyShortcut(int keyCode, KeyEvent event) {
  278.   // TODO Auto-generated method stub
  279.   return super.onKeyShortcut(keyCode, event);
  280. }
  281. public boolean onDown(MotionEvent e) {
  282.   // TODO Auto-generated method stub
  283.   return false;
  284. }
  285.    
  286. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ShapeDrawable是Android中的一个Drawable类型,它可以让开发者自定义绘制出各种形状的图形。下面是ShapeDrawable的用法: 1.创建一个ShapeDrawable对象 可以通过如下方式创建一个ShapeDrawable对象: ``` ShapeDrawable shapeDrawable = new ShapeDrawable(); ``` 2.设置ShapeDrawable的形状 在创建ShapeDrawable对象后,需要设置它的形状。ShapeDrawable支持以下几种形状: 矩形(RectangleShape): ``` shapeDrawable.setShape(new RectShape()); ``` 圆形(OvalShape): ``` shapeDrawable.setShape(new OvalShape()); ``` 线条(LineShape): ``` shapeDrawable.setShape(new LineShape()); ``` 圆角矩形(RoundRectShape): ``` float[] radii = {10, 10, 10, 10, 0, 0, 0, 0}; // 每个角的半径 RectF rectF = new RectF(0, 0, 100, 100); // 矩形的位置和大小 shapeDrawable.setShape(new RoundRectShape(radii, rectF, radii)); ``` 3.设置ShapeDrawable的颜色 设置ShapeDrawable的颜色可以使用setColor()方法: ``` shapeDrawable.getPaint().setColor(Color.RED); ``` 4.设置ShapeDrawable的边框 设置ShapeDrawable的边框可以使用setStroke()方法: ``` shapeDrawable.getPaint().setStrokeWidth(5); shapeDrawable.getPaint().setStyle(Paint.Style.STROKE); shapeDrawable.getPaint().setColor(Color.BLACK); ``` 5.使用ShapeDrawable 最后,可以将ShapeDrawable对象设置为View的背景或者ImageView的src属性,或者直接在自定义绘制的时候使用它。例如,在Activity的onCreate()方法中设置一个矩形ShapeDrawable的背景: ``` ShapeDrawable shapeDrawable = new ShapeDrawable(); shapeDrawable.setShape(new RectShape()); shapeDrawable.getPaint().setColor(Color.RED); View view = findViewById(R.id.view); view.setBackground(shapeDrawable); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值