Android 图形处理(二)

图片处理类

BitmapFactory(Bitmap的工具类)

         作用: 将来自于不同信息源的数据解析成Bitmap对象。

放法描述
decodeFile(String pathName)从指定的文件路径中解析并创建Bitmap
decodeFileDescriptor(FileDescriptor fd)从FileDescriptor对应的文件中解析并创建Bitmap对象
decodeResources(Resources res,int id)根据指定的id从资源中解析并创建Bitmap对象
decodeStream(InputStream is)用于从制定的输入源中解析并创建Bitmap对象

Bitmap(位图类)

         作用:
(1)获取图像信息,进行图像的剪切、缩放、移动、旋转等处理。
(2)指定格式保存图像文件

方法描述
compress(Bitmap.CompressFormat format , int quality, OutputStream o)将图片压缩为指定格式并保存,format参数为:Bitmap.CompressFormat.PNG、Bitmap.CompressFormat.JPEG、Bitmap.CompressFormat.WEBP
createBitmap(Bitmap source,int x, int y , int width,int heigth,Matrix m , boolean filter)从源图像的指定位置挖取一块创建新的Bitmap图像,并按Matrix指定规则进行变换
createBitmap(int width, int heigth,Bitmap.Config config)用于创建一个指定宽度与高度的新Bitmap对象
createBitmap(Bitmap source, int x,int y , int width,int heigth)从源图像的指定坐标挖取指定宽高的一块图像来创建新的Bitmap对象
createBitmap(int [] color,int width,int heigth,Bitmap.Config config)使用数组创建一个指定宽高的新Bitmap对象,其中,数组元素的个数为“width * heigth”
createBitmap(Bitmap src)使用源图像创建一个Bitmap
createScaledBitmap(Bitmap src,int dstWidth,int dstHeight,boolean filter)将源图像缩放为指定宽高的新的Bitmap对象
isRecycle()判断Bitmap对象是否进行回收
recycle()强制回收Bitmap()对象

BitmapFactory可以创建Bitmap对象供Bitmap使用

使用Bitmap画图时,仍要自己创建类并继承View,并重写onDraw()方法,
并在其中实例化Paint类用Canvas的draw方法画出来,其具体可看Android 图像处理(一)

这里列举Canvas使用Bitmap画图的常用方法:

绘制图片

可以将保存在Bitmap中的图像信息画出来

方法描述
drawBitmap(Bitmap bitmap,Rect src,RectF dst,Paint paint)从指定点绘制从源图像中挖出的一块
drawBitmap(Bitmap bitmap,float left,float top,Paint paint)在指定点绘制位图
drawBitmap(Bitmap bitmap,Rect src,Rect dst,Paint paint)从指定点绘制从源图像中挖出的一块

Rect src 表示要挖去的位置
Rect dst 表示要绘制的位置

例子:创建Bitmap对象通过BitmapFactory:(未使用布局文件,直接设置View为显示的内容)

MyView类

package com.example.newland.f20190818;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {
    public MyView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public MyView(Context context) {
        super(context);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/io.jpeg");
        canvas.drawBitmap(bitmap,25,85,paint);
    }
}

主活动类:

package com.example.newland.f20190818;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(MainActivity.this));
    }
}

记得加读取本地SD卡的权限:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

效果图:这是一张乱码图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值