從零開始學android<Bitmap圖形組件.四十七.>

來源:互聯網
上載者:User

標籤:接下來   建立   str   html   映像   dia   簡單   pac   too   

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內建播放器

從零開始學android&lt;Bitmap圖形組件.四十七.&gt;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.