Bitmap的基础综合

星期五竟然调课了,挺好的 连着三天不用上课,然后就接着学习Android 第九章,放假之前把它得结束了。

今天星期一,把这个周末写的综合一下就一起写了吧。


这几篇代码重复性很大 ,一会再说吧。。。


1.先看看旋转

1.1先把布局文件写出来 (下面的Demo都是一样的,就不重复写的

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout></span>

1.2 就写一个MainActivity类就行 

<span style="color:#660000;">package com.example.hejingzhou.matrixdemo;

</span><span style="font-size:18px;color:#336666;">import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {
    private FrameLayout frameLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frameLayout = (FrameLayout)findViewById(R.id.frameLayout);
        frameLayout.addView(new MainView(this));
    }
    public class MainView extends View
    {
        public MainView(Context context)
        {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {

            Paint paint = new Paint();
                                    //Bitmap工厂    资源解码
            Bitmap bitmap_backImage = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.cao);
            canvas.drawBitmap(bitmap_backImage, 0, 0, paint);//绘制背景图片(草原)
            Bitmap bitmap_rabbit = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.tu);
            canvas.drawBitmap(bitmap_rabbit,0,0,paint);//绘制原图(小兔子)
            //利用
            Matrix matrix = new Matrix();
            matrix.setRotate(30);//设置旋转(以(0,0)点为轴心旋转30度)   </span><span style="font-size:18px;color:#cc0000;">其实都是为这句服务的</span><span style="font-size:18px;color:#336666;">
            canvas.drawBitmap(bitmap_rabbit, matrix, paint);//绘制并应用matrix的变换
//            Matrix m = new Matrix();
//            m.setRotate(90,87,87);
//            canvas.drawBitmap(bitmap_rabbit,m,paint);
            super.onDraw(canvas);
        }
    }
}</span><span style="color:#660000;">
</span>

1.3效果



2、图形倾斜

2.1布局和上一个一样,不写了,ActivityJava代码也基本一样,就是MyView类中的onDraw方法区别

<span style="font-size:18px;color:#336666;">protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            Bitmap bitmap_backImage = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.cao);
            canvas.drawBitmap(bitmap_backImage,0, 0, paint);//

            Bitmap bitmap_rabbit = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.tu);
            Matrix matrix_1 = new Matrix();
            matrix_1.setSkew(2f,2f,50f,30f);//设置倾斜
            canvas.drawBitmap(bitmap_rabbit, matrix_1, paint);//绘制图像并应用Matrix

            Matrix matrix_2 = new Matrix();
            matrix_2.setSkew(-0.5f,0f,69,78);
            canvas.drawBitmap(bitmap_rabbit,matrix_2,paint);
            //canvas.drawBitmap(bitmap_rabbit,0,0,paint);//原图
            super.onDraw(canvas);
        }</span>

2.2效果





3、位图的平移

3.1布局文件一样 onDraw 方法不同

<span style="font-size:18px;">protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);

            Bitmap bitmap_backImage = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.cao);
            canvas.drawBitmap(bitmap_backImage, 0, 0, paint);

            Bitmap bitmap_rabbit = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.tu);
            canvas.drawBitmap(bitmap_rabbit,0,0,paint);//原图
            Matrix matrix_1 = new Matrix();
            //matrix_1.setRotate(30);//设置旋转
            matrix_1.postTranslate(800,200);//将matrix_1平移到(100,50)的位置 post平移
            canvas.drawBitmap(bitmap_rabbit,matrix_1,paint);

            super.onDraw(canvas);
        }</span>

3.2效果





4、位图缩放

4.1布局文件一样 onDraw 方法不

<span style="font-size:18px;"> protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            Bitmap bitmap_backImage = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.cao);
            canvas.drawBitmap(bitmap_backImage, 0, 0, paint);//设置背景图片

            Bitmap bitmap_rabbit = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.tu);

            Matrix matrix_2 = new Matrix();
            matrix_2.setScale(0.8f, 0.8f, 250, 800);//以(156,156)为轴心将图像在X轴和Y轴上均缩放80%
            canvas.drawBitmap(bitmap_rabbit,matrix_2,paint);
            //canvas.drawBitmap(bitmap_rabbit,0,0,paint);不经任何变化的图像

            Matrix matrix_0 = new Matrix();
            matrix_0.setScale(1.5f,1.5f,-730,-250);
            canvas.drawBitmap(bitmap_rabbit, matrix_0, paint);

            Matrix matrix_1 = new Matrix();
            matrix_1.setScale(2f,2f);//以(0,0点为轴心将图像在X轴和Y轴上均缩放200%)
            canvas.drawBitmap(bitmap_rabbit, matrix_1, paint);

            super.onDraw(canvas);
        }</span>

4.2效果




5、图片的平铺Mode

5.1这个布局文件一样,Activity就不太同了,这是是需要获取当前屏幕的长宽

<span style="color:#660000;">package com.example.hejingzhou.bitmapshder_1;

</span><span style="font-size:14px;"><span style="color:#3333ff;">import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

    private int view_width;
    private int view_height;
    private FrameLayout frameLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frameLayout = (FrameLayout)findViewById(R.id.frameLayout);
        frameLayout.addView(new MyView(this));
    }
    public class MyView extends View
    {
        public MyView(Context context)
        {
            super(context);
            </span><span style="color:#ff0000;">view_height = context.getResources().getDisplayMetrics().heightPixels;
            view_width = context.getResources().getDisplayMetrics().widthPixels;</span><span style="color:#3333ff;">
        }

        @Override
        protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);

            Bitmap bitmap_backimage = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.hua1);
            BitmapShader bitmapShader_1 = new BitmapShader(bitmap_backimage, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
            /**BitmapShader API:
             * Call this to create a new shader that will draw with a bitmap.
             *
             * @param bitmap            The bitmap to use inside the shader
             * @param tileX             The tiling mode for x to draw the bitmap in.
             * @param tileY             The tiling mode for y to draw the bitmap in.
             * */
//                    /**
//                    * replicate the edge color if the shader draws outside of its
//                    * original 边缘颜色
//                    */
//                    CLAMP   (0),
//                    /**
//                     * repeat the shader's image horizontally and vertically横向和纵向重复着色的图像 平铺
//                     */
//                    REPEAT  (1),
//                    /**
//                     * repeat the shader's image horizontally and vertically, alternating
//                     * mirror images so that adjacent images always seam横向和纵向重复着色器的图像,交/图·镜像,使相邻图像总是缝
//                     */
//                    MIRROR  (2);
            paint.setShader(bitmapShader_1);//设置渲染对象
            canvas.drawRect(0, 0, view_width,view_height, paint);//绘制一个矩形

            Bitmap bitmap_2 = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.hua2);
            BitmapShader bitmapShader_2 = new BitmapShader(bitmap_2, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
            paint.setShader(bitmapShader_2);
            RectF over = new RectF(0,0,280,180);//浮点矩形
            canvas.translate(40,20);//在画面上X轴上平移40像素,在Y轴上平移20像素
            /***
             * Matrix.postTranslate(40,20)
             * canvar.translate(40,20)    什么区别??????
             */
            canvas.drawOval(over,paint);

            super.onDraw(canvas);
        }
    }
}</span><span style="color:#660000;">
</span></span>

5.2参数不同效果展示  平铺 镜面





6、图形的圆角化

6.1布局一样 Java如下

<span style="color:#663366;">package com.example.hejingzhou.bitmapshader_2;

<span style="font-size:18px;">import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {
private FrameLayout frameLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frameLayout = (FrameLayout)findViewById(R.id.frameLayout);
        frameLayout.addView(new MyView(this));
        frameLayout.setBackgroundColor(Color.rgb(205,198,73));
    }
    public class MyView extends View
    {
        public MyView(Context context)
        {
            super(context);
        }
        @Override
        protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
//            Bitmap bitmap_1 = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.backimage);
//            canvas.drawBitmap(bitmap_1,0,0,paint);?????????为啥错呢!!
            RectF rectF_1 = new RectF(0,0,980,780);
            canvas.translate(40, 40);//平移向下
            paint.setStyle(Paint.Style.STROKE);//设置填充样式为描边
            paint.setColor(Color.RED);
            paint.setStrokeWidth(10);
            //    绘制圆形的矩形方法
            canvas.drawRoundRect(rectF_1, 40, 40, paint);//绘制一个描边的圆形矩形
            paint.setStyle(Paint.Style.FILL_AND_STROKE);//设置填充样式为填充
            Bitmap bitmap_mainImage = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.gou);
            BitmapShader bitmapShader_main = new BitmapShader(bitmap_mainImage, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
            paint.setShader(bitmapShader_main);
            canvas.drawRoundRect(rectF_1,40,40,paint);

            super.onDraw(canvas);
        }
    }
}</span></span><span style="color:#660000;">
</span>

6.2参数不同效果不同




7、这个用Android studio 开发时 出了一个错 就是Matrix类下过时了,按说能用但是方法确报错。后来只能用Eclipse做了。
Android Studio开发时产生这样的,问了一圈,不知道为什么。



??????????????????????????????????????????????
这次是做了单独的做了一个MyView 类  


7.1 先看看这个MyView 类吧  还是它重要

package com.example.magnifying;

<span style="font-size:18px;">import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View {
	private Bitmap bitmap;
	private ShapeDrawable drawable;
	private final int BANJING = 57;
	private final int BEISHU = 3;
	private Matrix matrix = new Matrix();
	private Bitmap bitmap_fangdajing;//放大的位图
	private int m_left = 0;//放大镜的左边距
	private int m_top = 0;//放大镜的上下边距
	
	public MyView(Context context) {
		
		super(context);
		Bitmap bitmap_backImage = BitmapFactory.decodeResource(getResources(), R.drawable.source);//需要放大图像
		bitmap = bitmap_backImage;
		/*
		 * 整个“放大”核心
		 * */
		BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(
				bitmap_backImage,
				bitmap_backImage.getWidth()*BEISHU,
				bitmap_backImage.getHeight()*BEISHU,
				true),
				TileMode.CLAMP,
				TileMode.CLAMP);
			/*
			 * createScaledBitmap在原来的位图基础创建一个新的位图
			 * 参数
			 * The source bitmap. 源位图 ----BackImage
			 * The new bitmap's desired width. 新的位图所需的宽度。
			 * The new bitmap's desired height. 新的位图所需的高度。
			 * 如果源筛选  就写true。
			 * */
	/*public   BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)
	调用这个方法来产生一个画有一个位图的渲染器(Shader)。
	bitmap   在渲染器内使用的位图
	tileX      The tiling mode for x to draw the bitmap in.   在位图上X方向花砖模式
	tileY     The tiling mode for y to draw the bitmap in.    在位图上Y方向花砖模式
	TileMode:(一共有三种)
	CLAMP  :如果渲染器超出原始边界范围,会复制范围内边缘染色。
	REPEAT :横向和纵向的重复渲染器图片,平铺。
	MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT 重复方式不一样,他是以镜像方式平铺。
	*/	
		/*绘制一个椭圆形
		 * */
		drawable  = new ShapeDrawable(new OvalShape());
		drawable.getPaint().setShader(shader);//设置渲染器
		drawable.setBounds(0, 0, BANJING*2,BANJING*2);//设置圆外切的矩形
		
		bitmap_fangdajing = BitmapFactory.decodeResource(getResources(), 
				R.drawable.magnifier);//获取放大镜的图像
		m_left = BANJING - bitmap_fangdajing.getWidth()/2;//计算放大镜的左右边距
		m_top = BANJING - bitmap_fangdajing.getHeight()/4;//计算放大镜的上下边距
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawBitmap(bitmap, 0,0,null);//画背景
		canvas.drawBitmap(bitmap_fangdajing,m_left,m_top,null);//绘制放大镜
		drawable.draw(canvas);
	}

	/*
	 * 触摸放大镜移动  
	 * */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		final int x = (int)event.getRawX();
		final int y = (int)event.getRawY();
		
		matrix.setTranslate(BANJING - x*BEISHU, BANJING-y*BEISHU);//设置远的外切矩形
		drawable.getPaint().getShader().setLocalMatrix(matrix);
		drawable.setBounds(x-BANJING,y-BEISHU,x+BANJING,y+BANJING);
		
		m_left = x - bitmap_fangdajing.getWidth()/2;
		m_top = y - bitmap_fangdajing.getHeight()/2;
		invalidate();
	
		return true;
	}
	 
}
</span>



7.2 MainActivity

<span style="color:#990000;">package com.example.magnifying;

</span><span style="font-size:18px;color:#cc33cc;">import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends Activity {
	private FrameLayout frameLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frameLayout = (FrameLayout)findViewById(R.id.frameLayout);
        frameLayout.addView(new MyView(this));
    }

    
}</span><span style="color:#990000;">
</span>

7.3效果









综合源代码:          http://download.csdn.net/detail/csdnhejingzhou/9355255  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值