zoukankan      html  css  js  c++  java
  • 从零開始学android<Bitmap图形组件.四十七.>

    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
    	}
    
    	@Override
    	protected 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自带播放器

  • 相关阅读:
    《C语言》for语句(8)
    解决vue vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in nextTick: “TypeError: Cannot convert undefine
    React中WebSocket使用以及服务端崩溃重连
    React Native 中 react-navigation 导航器的使用 [亲测可用]
    ueditor 修改内容方法报错no funtion解决方式
    nodeJs与elementUI实现多图片上传
    Vue多页面开发案例
    Vue.js Cli 3.0 多页面开发案例解析
    基于node.js 微信支付notify_url回调接收不到xml
    react-image-gallery 加入视频图片混合显示
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6852574.html
Copyright © 2011-2022 走看看