`

android 图像

 
阅读更多

自定义图像,继承view,实现ondraw()方法

 

实现位图的三种常用方法:

 

用BitmapFactory解析图片 
// --> 使用BitmapFactory解析图片 
           public void myUseBitmapFactory(Canvas canvas){ 
           // 定义画笔 
              Paint paint = new Paint(); 
           // 获取资源流 
              Resources rec = getResources(); 
              InputStream in = rec.openRawResource(R.drawable.haha); 
           // 设置图片 
              Bitmap bitmap =BitmapFactory.decodeStream(in); 
           // 绘制图片 
              canvas.drawBitmap(bitmap, 0,20, paint);          
           }
二、使用BitmapDrawable解析图片 
         // --> 使用BitmapDrawable解析图片 
           public void myUseBitmapDrawable(Canvas canvas){ 
           // 定义画笔 
              Paint paint = new Paint(); 
           // 获得资源 
              Resources rec = getResources(); 
           // BitmapDrawable 
              BitmapDrawable bitmapDrawable = (BitmapDrawable) rec.getDrawable(R.drawable.haha); 
           // 得到Bitmap 
              Bitmap bitmap = bitmapDrawable.getBitmap(); 
           // 在画板上绘制图片 
              canvas.drawBitmap(bitmap, 20,120,paint); 
           }
 三、使用InputStream和BitmapDrawable绘制 
        // --> 使用InputStream和BitmapDrawable解析图片 
           public void myUseInputStreamandBitmapDrawable(Canvas canvas){ 
           // 定义画笔 
              Paint paint = new Paint(); 
           // 获得资源 
              Resources rec = getResources(); 
           // InputStream得到资源流 
              InputStream in = rec.openRawResource(R.drawable.haha); 
           // BitmapDrawable 解析数据流 
              BitmapDrawable bitmapDrawable =  new BitmapDrawable(in); 
           // 得到图片 
              Bitmap bitmap = bitmapDrawable.getBitmap(); 
           // 绘制图片 
              canvas.drawBitmap(bitmap, 100, 100,paint); 
           }

 

 

引用自定义标签的两种方式:

 

    <com.fxhy.stady.HelloView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 


    <view class="com.fxhy.stady.HelloView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics