从零开始学android<Bitmap图形组件.四十七.>

来源:互联网 发布:知乎 大胸妹子 编辑:程序博客网 时间:2024/05/16 18:33
android.graphics.Bitmap(位图)是Android手机中专门提供的用于操作图片资源的操作类,使用此类可以直接从资源文件之中进行图片资源的读取,并且对这些图片进行一些简单的修改。

常用的方法
1
public static Bitmap createBitmap (Bitmap src)
普通
复制一个Bitmap
2
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
普通
对一个Bitmap进行剪切
3
public final int getHeight()
普通
取得图像的高
4
public final int getWidth()
普通
取得图像的宽
5
public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
普通
创建一个指定大小的Bitmap

接下来用简单的例子来进行说明

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <com.example.bitmap1.MyView        android:layout_width="wrap_content"        android:layout_height="wrap_content"></com.example.bitmap1.MyView></RelativeLayout>


主程序

package com.example.bitmap1;import android.os.Bundle;import android.app.Activity;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);      }    }

MyView中定义的bitmap


package com.example.bitmap1;              import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class MyView extends View {public MyView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stub//获取图片文件Bitmap bitmap = BitmapFactory.decodeResource(super.getResources(),R.drawable.a4);//设置背景画布颜色canvas.drawColor(Color.WHITE);//初始化画笔Paint paint=new Paint();//设置边缘羽化paint.setAntiAlias(true);//绘制图片canvas.drawBitmap(bitmap, 0, 0,paint );//设置画笔颜色paint.setColor(Color.BLUE);//设置字体尺寸paint.setTextSize(20);//绘制文字canvas.drawText("我的头像", 10, bitmap.getHeight()+20, paint);}}


虽然有点丑,但是绘制的还不错,对bitmap的操作还有许多,大家可以参照api自行进行学习,图形的绘制在游戏和APP引导用的比较多


下节预报:Mediaplayer自带播放器

0 0