题目来自:《android高薪之路—android程序员面试宝典》一书  ,只是为了个人学习方便

题目:在android工程的res/drawable目录中有一个android.png图像文件,如何将该图像显示在View上

直接代码:

 

     
  1. package com.app;  
  2.  
  3. import java.io.InputStream;  
  4. import android.content.Context;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.graphics.BitmapFactory.Options;  
  8. import android.graphics.Canvas;  
  9. import android.graphics.Paint;  
  10. import android.graphics.drawable.Drawable;  
  11. import android.view.View;  
  12.  
  13. public class GetBitmapView extends View{  
  14.  
  15.        public GetBitmapView(Context context) {  
  16.              super(context);  
  17.       }  
  18.  
  19.        @Override  
  20.        protected void onDraw(Canvas canvas) {  
  21.              super.onDraw(canvas);  
  22.             Paint paint = new Paint();  
  23.             InputStream is = getResources().openRawResource(R.drawable.android);  
  24.             Options opts = new Options();  
  25.             opts. inSampleSize=2;  
  26.             Bitmap bitmap = BitmapFactory. decodeStream(is, null, opts);  
  27.             canvas.drawBitmap(bitmap, 10, 10, paint);  
  28.               
  29.             Drawable drawable = getResources().getDrawable(R.drawable.android);  
  30.             drawable.setBounds(50, 350, 180, 420);  
  31.             drawable.setAlpha(100);  
  32.             drawable.draw(canvas);  
  33.       }  
  34. }  

 

转载于:https://blog.51cto.com/3599672/941098

更多相关文章

  1. Android游戏开发实践指南(华章程序员书库)
  2. Android(安卓)程序员们的现状通病!
  3. 都2020年了你居然还学Android?程序员没有出路了!
  4. .Net程序员玩转Android系列之一~Java快速入门
  5. 红透半边天的Android
  6. 几行代码看程序员的水平——Android文件命名规范
  7. Android(安卓)面试精华题目总结
  8. Mac OS升级到Yosemite后一些问题
  9. 同样的互联网大环境下,你连工作都找不到,年薪该拿60w的程序员他还

随机推荐

  1. Android开机广播android.intent.action.B
  2. Andorid旋转动画
  3. Android摄像头编程及注意事项
  4. Android6.0 MTK 需求文档(五)
  5. Android 自带TTS 使用
  6. android判断APP是否已经安装
  7. android下载
  8. Android(安卓)快速开发框架——————
  9. Android通过WebView调用 JS 代码
  10. android 删除文件,打开指定的文件类型