有关位图的几点总结
        【摘要】 
                    位图是我们开发中最常用的资源 
1. 从资源中获取位图 可以使用BitmapDrawable或者BitmapFactory来获取资源中的位图。 首先,需要获取资源:  
Resources res=getResources();(1)使用BitmapDrawable获取位图
 
// 读取InputStream并得到位图InputSt...
    
    
    
    位图是我们开发中最常用的资源
1. 从资源中获取位图
 可以使用BitmapDrawable或者BitmapFactory来获取资源中的位图。
 首先,需要获取资源:
 
Resources res=getResources();
 
  
   - 
    
     
    
    
     
      // 读取InputStream并得到位图
     
    
- 
    
     
    
    
     
      InputStream is=res.openRawResource(R.drawable.pic);
     
    
- 
    
     
    
    
     
      BitmapDrawable bmpDraw=new BitmapDrawable(is);
     
    
- 
    
     
    
    
     
      Bitmap bmp=bmpDraw.getBitmap();
     
    
 
  
   - 
    
     
    
    
     
      BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic);
     
    
- 
    
     
    
    
     
      Bitmap bmp=bmpDraw.getBitmap();
     
    
 包括文件,流,字节数组
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
 
  
   - 
    
     
    
    
     
      public class MainActivity extends Activity {
     
    
- 
    
     
    
    
     
          /** Called when the activity is first created. */
     
    
- 
    
     
    
    
     
          @Override
     
    
- 
    
     
    
    
     
          public void onCreate(Bundle savedInstanceState) {
     
    
- 
    
     
    
    
     
              super.onCreate(savedInstanceState);
     
    
- 
    
     
    
    
     
              setContentView(new BmpPanel(this));
     
    
- 
    
     
    
    
     
          }
     
    
- 
    
     
    
    
     
          
     
    
- 
    
     
    
    
     
          class BmpPanel extends View{               
     
    
- 
    
     
    
    
     
              public BmpPanel(Context context) {  
     
    
- 
    
     
    
    
     
                  super(context); 
     
    
- 
    
     
    
    
     
              }      
     
    
- 
    
     
    
    
     
              public void onDraw(Canvas canvas){  
     
    
- 
    
     
    
    
     
                  Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic);  
     
    
- 
    
     
    
    
     
                  //canvas.drawColor(Color.BLACK);  
     
    
- 
    
     
    
    
     
                  canvas.drawBitmap(bmp, 10, 10, null);  
     
    
- 
    
     
    
    
     
              }  
     
    
- 
    
     
    
    
     
          } 
     
    
- 
    
     
    
    
     
      }
     
    
 
  
   - 
    
     
    
    
     
              // 获取位图
     
    
- 
    
     
    
    
     
              Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
     
    
- 
    
     
    
    
     
              // 转换为BitmapDrawable对象
     
    
- 
    
     
    
    
     
              BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
     
    
- 
    
     
    
    
     
              // 显示位图
     
    
- 
    
     
    
    
     
              ImageView iv = (ImageView)findViewById(R.id.ImageView);
     
    
- 
    
     
    
    
     
             iv.setImageDrawable(bmpDraw);
     
    
 
  
   - 
    
     
    
    
     
                  Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic);  
     
    
- 
    
     
    
    
     
                  Matrix matrix=new Matrix();
     
    
- 
    
     
    
    
     
                  matrix.postScale(0.5f, 0.5f);
     
    
- 
    
     
    
    
     
                  Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);
     
    
- 
    
     
    
    
     
                  //canvas.drawColor(Color.BLACK);  
     
    
- 
    
     
    
    
     
                  canvas.drawBitmap(dstbmp, 10, 10, null);
     
    
 
  
   - 
    
     
    
    
     
                  Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic);  
     
    
- 
    
     
    
    
     
                  Matrix matrix=new Matrix();
     
    
- 
    
     
    
    
     
                  matrix.postScale(0.8f, 0.8f);
     
    
- 
    
     
    
    
     
                  matrix.postRotate(45);
     
    
- 
    
     
    
    
     
                  Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
     
    
- 
    
     
    
    
     
                  bmp.getHeight(),matrix,true);
     
    
- 
    
     
    
    
     
                  canvas.drawColor(Color.BLACK); 
     
    
- 
    
     
    
    
     
                  canvas.drawBitmap(dstbmp, 10, 10, null); 
     
    
 
  
   - 
    
     
    
    
     
          * /** 
     
    
- 
    
     
    
    
     
          *    * create the bitmap from a byte array 
     
    
- 
    
     
    
    
     
          *    *
     
    
- 
    
     
    
    
     
          *    * @param src the bitmap object you want proecss
     
    
- 
    
     
    
    
     
          *    * @param watermark the water mark above the src
     
    
- 
    
     
    
    
     
          *    * @return return a bitmap object ,if paramter's length is 0,return null
     
    
- 
    
     
    
    
     
          *    */ 
     
    
- 
    
     
    
    
     
          *    private Bitmap createBitmap( Bitmap src, Bitmap watermark )  
     
    
- 
    
     
    
    
     
          *    {  
     
    
- 
    
     
    
    
     
          *        String tag = "createBitmap";  
     
    
- 
    
     
    
    
     
          *        Log.d( tag, "create a new bitmap" );  
     
    
- 
    
     
    
    
     
          *        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, h, Config.ARGB_8888 );//创建一个和src大小一样的空位图  
     
    
- 
    
     
    
    
     
          *        Canvas cv = new Canvas( newb );  
     
    
- 
    
     
    
    
     
          *        //draw src into  
     
    
- 
    
     
    
    
     
          *        cv.drawBitmap( src, 0, 0, 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;  
     
    
- 
    
     
    
    
     
          *    }
     
    
 
  
   - 
    
     
    
    
     
      int px = getMeasuredWidth();
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      int py = getMeasuredWidth();
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      // Draw background
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      canvas.drawRect(0, 0, px, py, backgroundPaint);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      canvas.save();
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      canvas.rotate(90, px/2, py/2);                
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      // Draw up arrow
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      canvas.drawLine(px / 2, 0, 0, py / 2, linePaint);                
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      canvas.drawLine(px / 2, 0, px, py / 2, linePaint);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      canvas.drawLine(px / 2, 0, px / 2, py, linePaint);
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      canvas.restore();
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      // Draw circle
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      canvas.drawCircle(px - 10, py - 10, 10, linePaint);
     
    
  
如果我们不调用save和restore会是什么样子呢?如图2所示:
 
从这两个图中,我们就能看到圆圈位置的明显差异。不进行Canvas的save和restore操作的话,所有的图像都是在画布旋转90°后的画布上绘制的。当执行完onDraw方法,系统自动将画布恢复回来。save和restore操作执行的时机不同,就能造成绘制的图形不同。
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/8557393
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)