20190110

作者: 糖_分 | 来源:发表于2019-01-21 10:08 被阅读0次

看看这本书 -《Android游戏编程之从零开始》

Day_3 2019/01/21


Bitmap 位图的渲染与操作

首先,最简单的如何生成一张位图,如下

Bitmap bitmap = BitmapFactory.decodeResource( this.getResources() , R.mipmap.ic_launcher );

下面是常用的操作函数

public class BitmapSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
    private SurfaceHolder holder;
    private Canvas canvas;
    private Bitmap bitmap = BitmapFactory.decodeResource( this.getResources() , R.mipmap.ic_launcher );
    private Paint paint;
    
    public BitmapSurfaceView( Context context )
    {
        super( context );
        holder = this.getHolder();
        holder.addCallback( this );
        setFocusable( true );
        paint = new Paint();
        paint.setColor( Color.BLACK );
    }
    
    @Override
    public void surfaceCreated( SurfaceHolder surfaceHolder )
    {
        myDraw();
    }
    
    private void myDraw()
    {
        try
        {
            canvas = holder.lockCanvas();
            if( canvas != null )
            {
                //用于保存当前画布的状态
                canvas.save();
                //旋转方式1
                canvas.rotate( 30 , bitmap.getWidth() / 2 , bitmap.getHeight() / 2 );
                canvas.drawBitmap( bitmap , 0 , 0 , paint );
                //用于恢复上次保存的画布状态
                canvas.restore();
                canvas.drawBitmap( bitmap , 100 , 0 , paint );
                //旋转方式2
                Matrix mx = new Matrix();
                mx.postRotate( 30 , bitmap.getWidth() / 2 , bitmap.getHeight() / 2 );
                canvas.drawBitmap( bitmap , mx , paint );
                //平移位图方式1
                canvas.save();
                //平移距离可小于0,表示往左移动
                canvas.translate( 10 , 10 );
                canvas.drawBitmap( bitmap , 0 , 0 , paint );
                canvas.restore();
                //平移位图方式2
                Matrix matrix = new Matrix();
                matrix.postTranslate( 10 , 10 );
                canvas.drawBitmap( bitmap , matrix , paint );
                //缩放位图
                canvas.save();
                //============x轴缩放比例===y轴的===========x轴起始点==========================y轴起始点
                canvas.scale( 2f , 2f , 50 + bitmap.getWidth() / 2 , 50 + bitmap.getHeight() / 2 );
                canvas.drawBitmap( bitmap , 50 , 50 , paint );
                canvas.restore();
                canvas.drawBitmap( bitmap , 50 , 50 , paint );
                //x轴镜像
                canvas.save();
                canvas.scale( -1 , 1 , bitmap.getWidth() / 2 , bitmap.getHeight() / 2 );
                canvas.drawBitmap( bitmap , 0 , 0 , paint );
                canvas.restore();
                //y轴镜像
                canvas.save();
                canvas.scale( 1 , -1 , bitmap.getWidth() / 2 , bitmap.getHeight() / 2 );
                canvas.drawBitmap( bitmap , 0 , 0 , paint );
                canvas.restore();
                //x轴镜像
                canvas.drawBitmap( bitmap , 0 , 0 , paint );
                Matrix matrix1 = new Matrix();
                //使用矩阵的移动方法
                matrix.postTranslate( 100 , 100 );
                matrix.postScale( -1 , 1 , 100 + bitmap.getWidth() / 2 , 100 + bitmap.getHeight() / 2 );
                canvas.drawBitmap( bitmap , matrix1 , paint );
                //y轴镜像
                canvas.drawBitmap( bitmap , 0 , 0 , paint );
                Matrix matrix2 = new Matrix();
                //使用矩阵的移动方法
                matrix2.postTranslate( 100 , 100 );
                matrix2.postScale( 1 , -1 , 100 + bitmap.getWidth() / 2 , 100 + bitmap.getHeight() / 2 );
                canvas.drawBitmap( bitmap , matrix2 , paint );
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        finally
        {
            if( canvas != null )
            {
                holder.unlockCanvasAndPost( canvas );
            }
        }
    }
    
    @Override
    public void surfaceChanged(
            SurfaceHolder surfaceHolder ,
            int i ,
            int i1 ,
            int i2 )
    {
    
    }
    
    @Override
    public void surfaceDestroyed( SurfaceHolder surfaceHolder )
    {
    
    }
}

相关文章

  • test1 - 草稿

    1177 20190110

  • 20190110酒

    20190110酒 坚持写作第120天。

  • Android之ConstraintLayout Demo(20

    Android之ConstraintLayout Demo(20190110) 参考文章:https://www....

  • 20190110

  • 20190110

    今天下班挺早的,从网上买的书到货了,搬回家去,寒假再看。 等我到家发现桌子上没有饭,心想老妈这是坚...

  • 20190110

    我今天听到一个很悲伤的故事,我很难过。自认为自己过的生活很悲剧,可是这个世界真的好黑暗啊,我在阳光下,我看到了什么...

  • 20190110

    这两天看了两场沙龙的内容:①吴霭仪谈“查良镛与金庸”——现实与江湖 ②梁文道谈“在北京看香港”,以及周保松教授的「...

  • 20190110

    离考试越来越近了,儿子的成绩忽高忽低,我已经没有办法了,昨天他爸爸回来又给他做了一会思想工作,也不知道管用吧,我现...

  • 20190110

    思想:林则徐说过这么一句话:“海納百川,有容乃大。"与人交往,有一分退让就受一分益;吃分亏就积一分福。反之,存一分...

  • 20190110

    备战重马第10天 雨天。游泳课1小时。 为了练习蝶泳的划水动作。腿夹浮板,手划水,在...

网友评论

      本文标题:20190110

      本文链接:https://www.haomeiwen.com/subject/gteorqtx.html