绘制picture
2015-11-19 16:27
190 查看
package com.example.xfermodesdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Picture; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.graphics.drawable.PictureDrawable; import android.os.Bundle; import android.view.View; public class PicturesActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new SampleView(this)); } private static class SampleView extends View { private Picture mPicture; private Drawable mDrawable; public SampleView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); mPicture = new Picture(); /** * Canvas android.graphics.Picture.beginRecording(int width, int * height) * * To record a picture, call beginRecording() and then draw into the * Canvas that is returned. Nothing we appear on screen, but all of * the draw commands (e.g. drawRect(...)) will be recorded. To stop * recording, call endRecording(). At this point the Canvas that was * returned must no longer be referenced, and nothing should be * drawn into it */ drawSomething(mPicture.beginRecording(200, 100)); /** * void android.graphics.Picture.endRecording() * * * Call endRecording when the picture is built. After this call, the * picture may be drawn, but the canvas that was returned by * beginRecording must not be referenced anymore. This is * automatically called if Picture.draw() or Canvas.drawPicture() is * called. */ mPicture.endRecording(); mDrawable = new PictureDrawable(mPicture); } static void drawSomething(Canvas canvas) { Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); p.setColor(0x88FF0000); canvas.drawCircle(50, 50, 40, p); p.setColor(Color.GREEN); p.setTextSize(30); canvas.drawText("Pictures", 60, 60, p); } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); canvas.drawPicture(mPicture);// 1.绘制图片 canvas.drawPicture(mPicture, new RectF(0, 100, getWidth(), 200));// 2.绘制拉伸的图片 mDrawable.setBounds(0, 200, getWidth(), 300); mDrawable.draw(canvas);// 3.绘制图片 ByteArrayOutputStream os = new ByteArrayOutputStream(); mPicture.writeToStream(os); InputStream is = new ByteArrayInputStream(os.toByteArray()); canvas.translate(0, 300); canvas.drawPicture(Picture.createFromStream(is));// 4。绘制图片 } } }
相关文章推荐
- JSON.Stringify()函数使用心得
- 【Android自定义Dialog】电视机开关效果的Dialog
- C/C++ 指针做参数 堆内存分配
- Android Fragment 真正的完全解析(下)
- 【转】Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
- 写一个Windows上的守护进程(8)获取进程路径
- 信息安全系统设计基础第十一周学习总结
- 解决Win10 svn图标不显示
- 关于UIAlertController的whose view is not in the window hierarchy!问题解决
- ORACLE 11G新特性2
- RecyclerView的简单使用
- Tomcat8启动闪退问题的解决办法
- 存储基础知识4——
- Asp.Net通过ODBC连接Oracle数据库
- Ubutun14.04下vnc花屏的解决
- javascript中var that=this
- Android UI控件之Spinner实现省市区三级联动
- Windows Phone8应用添加KeyMob管理库教程
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )屏幕适配
- iPhone 应用里实现截屏功能的代码