canvas教程

Android 画出图片

字号+ 作者:H5之家 来源:H5之家 2015-10-08 15:02 我要评论( )

Android 画出图片 我们主要介绍了如何装载一张图片,然后把它画在屏幕上。android 可以通过 xml 来配置 装载图片,比如用 ImageButtion ImageButtion,然后程序装

Android 画出图片


Android 画出图片 我们主要介绍了如何装载一张图片,然后把它画在屏幕上。android 可以通过 xml 来配置 装载图片,比如用 ImageButtion ImageButtion,然后程序装载这个 xml 资源 资源,也可以实现,那样更简单, 大家看见我这么一说就应该明白点了, 其实这个小实例真的是很简单的, 那么就来看看这个 是怎么样来实现的吧: 1:首先创建一个类

drawBitmap: 代码: java 代码: 1. package eoe.drawBitmap; 2. 3. import android.app.Activity; 4. import android.os.Bundle; 5. 6. 7. public class drawBitmap extends Activity { 8. /** Called when the activity is first created. */ 9. 10. drawAction da; 11. public void onCreate(Bundle icicle) { 12. super.onCreate(icicle); 13. da = new drawAction(this); 14. setContentView(da); 15. } 16. } 复制代码 2:创建一个画图类 drawAction 代码: java 代码: 1. package eoe.drawBitmap; 2. 3. import android.content.Context; 4. import android.content.Resources; 5. import android.graphics.Bitmap; 6. import android.graphics.Canvas; 7. import android.graphics.Paint; 8. import android.graphics.drawable.Drawable; 9. import android.view.View; 10. 11. 12. public class drawAction extends View {

13. Paint paint = new Paint(); 14. Bitmap me = null; 15. 16. public drawAction(Context context) { 17. 18. super(context); 19. 20. me = loadImage(R.drawable.me1, 240, 180); 21. 22. // TODO Auto-generated constructor stub 23. } 24. 25. protected void onDraw(Canvas canvas) { 26. canvas.drawBitmap(me, 0, 0, paint); 27. 28. } 29. 30. public Bitmap loadImage(int fileid, int width, int height) { 31. Resources r = this.getContext().getResources(); 32. Bitmap bitmap = Bitmap.createBitmap(width, height, true); 33. Drawable drawable = r.getDrawable(fileid); 34. Canvas canvas = new Canvas(bitmap); 35. 36. drawable.setBounds(0, 0, width, height); 37. drawable.draw(canvas); 38. 39. return bitmap; 40. } 41. 42. } 复制代码 loadImage 是俺临时封装得一个函数,用于加载图形文件,比较简陋,哪位同学有 兴趣,自己可以创造更优秀得函数。 canvas.drawBitmap(me, 0, 0, paint);,调用了系统本身得 drawBitmap 函数,这 个函数其实也可以逻辑封装,用起来会更方便。 extends View 继承 View,就必须在本类构造函数 super(context);这么一下。


Android 画出图片

第一范文网-第一范文网包含各类年终总结范文,工作总结范文,个人简历范文,述职报告范文,检讨书范文,通过收集整理大量专业知识,职业资料、考试资料,考试复习指导,试题资料等给大家分享;同时提供学习互动交流;更好的帮助大家学习。

点击搜索更多“Android 画出图片”相关的内容
下载《Android 画出图片》

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Canvas与ValueAnimator

    Canvas与ValueAnimator

    2017-04-28 18:00

  • Android Bitmap和Canvas学习笔记(转)

    Android Bitmap和Canvas学习笔记(转)

    2017-04-28 17:00

  • 21天学习android开发教程之SurfaceView与多线程的混搭

    21天学习android开发教程之SurfaceView与多线程的混搭

    2017-04-27 12:00

  • Android画图学习免费下载

    Android画图学习免费下载

    2017-04-27 11:01

网友点评