Android 在放大Bitmap画布并在Bitmap上绘制矩形

 

首先是显示 ImageView的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
   <ImageView
    android:id="@+id/image"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="#999999"
    >
    </ImageView>
    <ImageView
    android:id="@+id/image1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="#999999"
    >
    </ImageView>
</LinearLayout>

 

 二:

public class IconActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        ImageView image=(ImageView) this.findViewById(R.id.image);
        Bitmap bm=readBitMap(this,R.drawable.test_launcher);
        Bitmap delete=readBitMap(this,R.drawable.delete);
        image.setImageBitmap(bm);
       
        ImageView image1=(ImageView) this.findViewById(R.id.image1);
        Bitmap bm1=createBitmap(bm);
       
       
        image1.setImageBitmap(createBitmapNew(bm1));  
      
    }
   
   
  
    public  Bitmap readBitMap(Context context, int resId){ 

           BitmapFactory.Options opt = new BitmapFactory.Options(); 

           opt.inPreferredConfig = Bitmap.Config.RGB_565;  

           opt.inPurgeable = true; 

           opt.inInputShareable = true; 

          //获取资源图片 

           InputStream is = context.getResources().openRawResource(resId); 

           return BitmapFactory.decodeStream(is,null,opt); 

       }
   
  
    //将画布放大
  // private Bitmap createBitmap(Bitmap src, Bitmap watermark )
    private Bitmap createBitmap(Bitmap src)
    {
    if( src == null )

    {

    return null;

    }

    int w = src.getWidth();

    int h = src.getHeight();

    //int ww = watermark.getWidth();

    //int wh = watermark.getHeight();

    //create the new blank bitmap

    Bitmap newb = Bitmap.createBitmap(w*3/2, h*3/2, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图

    Canvas cv = new Canvas(newb );

    //draw src into

    cv.drawBitmap(src, w/6, h/4, null );//在 0,0坐标开始画入src

    //draw watermark into

    //cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印

    //save all clip

    cv.save( Canvas.ALL_SAVE_FLAG );//保存

    //store

    cv.restore();//存储

    return newb;

    }
    
    
    //在Bitmap 上绘制矩形块
    private Bitmap createBitmapNew(Bitmap src)
    {
    if( src == null )

    {

    return null;

    }

    int w = src.getWidth();

    int h = src.getHeight();
    Paint mPaint = new Paint(); 
;

    Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图

    Canvas cv = new Canvas(newb);
   
    //Canvas cv1=

   // cv.drawBitmap(src, w/6, h/4, null );//在 0,0坐标开始画入src
   
    mPaint.setColor(Color.GREEN); 
    cv.drawRect(new Rect(w*2/3, 0,  w, h/2), mPaint); 
 
    //draw watermark into

    //cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印

    //save all clip

    cv.save(Canvas.ALL_SAVE_FLAG );//保存

    //store

    cv.restore();//存储

    return newb;

    }
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值