android draw bitmap 示例代码

package com.testDraw;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class testDraw extends Activity {
   
     ImageView iv;
     Button btn1,btn2,btn3,btn4;
     Resources r;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        iv=(ImageView)this.findViewById(R.id.ImageView01);
        btn1=(Button)this.findViewById(R.id.Button01);
        btn2=(Button)this.findViewById(R.id.Button02);
        btn3=(Button)this.findViewById(R.id.Button03);

        btn1.setOnClickListener(new ClickEvent());
        btn2.setOnClickListener(new ClickEvent());
        btn3.setOnClickListener(new ClickEvent());
       
        r = this.getResources();

 
    }
     class ClickEvent implements View.OnClickListener {

          public void onClick(View v) {
               if(v==btn1)//显示资源图片
               {//功能等效
                    //iv.setBackgroundResource(R.drawable.icon);//打开资源图片
                    Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);//打开资源图片
                    iv.setImageBitmap(bmp);
               }
               else if(v==btn2)//显示并绘画资源图片
               {
                  Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);//只读,不能直接在bmp上画
                  Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );
                 
                  Canvas canvasTemp = new Canvas( newb );
                  canvasTemp.drawColor(Color.TRANSPARENT);
                 
                  Paint p = new Paint();
                  String familyName ="宋体";
                  Typeface font = Typeface.create(familyName,Typeface.BOLD);
                  p.setColor(Color.RED);
                  p.setTypeface(font);
                  p.setTextSize(22);
                  canvasTemp.drawText("写字。。。",50,50,p);
                  canvasTemp.drawBitmap(bmp, 50, 50, p);//画图
                  iv.setImageBitmap(newb);
               }
               else if(v==btn3)//直接在Button上绘图
               {
                    Bitmap newb = Bitmap.createBitmap( btn3.getWidth(), btn3.getHeight(), Config.ARGB_8888 );
                    Canvas canvasTemp = new Canvas( newb );
                   canvasTemp.drawColor(Color.WHITE);
                   Paint p = new Paint();
                    String familyName = "宋体";
                    Typeface font = Typeface.create(familyName, Typeface.BOLD);
                    p.setColor(Color.RED);
                    p.setTypeface(font);
                    p.setTextSize(20);
                    canvasTemp.drawText("写字。。。", 30, 30, p);
                    Drawable drawable = new BitmapDrawable(newb);
                    btn3.setBackgroundDrawable(drawable);
               }
          }
         
     }

}


转载于:https://my.oschina.net/lao4/blog/341221

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值