1. 在畫布上繪製(Draw with a Canvas)
Android提供了自訂的繪製API,可以繪製各種圖形,文字,映像對象在Canvas上,而事
實上所有在Canvas對想都會被繪製到Canvas底層的Bitmap對象上面,建立一個Canvas
對象的語句如下:
Bitmap b =Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c =new Canvas(b);
首先建立一個32位的位元影像對象,類型是ARGB,然後在這個位元影像對象上建立一個畫布(Canvas)
對象。
view.draw(c);
FileOutputStreamfos = new FileOutputStream (new File (APP_FILE_PATH +"/Canvastoimage.png"));
bitmap.compress(Bitmap.CompressFormat.JPEG,100, fos);
將View中內容輸出到映像,壓縮映像到指定格式JPG.其中fos為檔案輸出資料流。
在Android對象上顯示Canvas的內容,要重載View的onDraw()方法,這點跟Java Swing
中要重寫JComponent的paintComponent()方法有點類似,這樣可以擷取圖形系統的繪製對
象,從而實現自訂對象的繪製。一個完整的重寫View實現Canvas繪製顯示的代碼如下:
package com.gloomyfish;import android.app.Activity;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.graphics.Paint.Style;import android.os.Bundle;import android.view.View;import android.view.Window;public class GraphicHelloActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(new Panel(this)); } class Panel extends View { public Panel(Context context) { super(context); } @Override public void onDraw(Canvas canvas) { Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.flower_001); canvas.drawColor(Color.WHITE); canvas.drawBitmap(_scratch, 0, 0, null); Paint paint = new Paint(); paint.setColor(Color.GREEN); paint.setStyle(Style.FILL); paint.setTextSize(16); canvas.drawText("Hello, Android Canvas", 20, 80, paint); } }}
程式效果如下:
2. 使用可繪製對象(Drawables Object)
Android中還提供一種相對簡單和便捷的API用來讀寫和顯示影像檔,就是以resource的形式載入圖片對象,
使用ImageView API來對映像顯示,如果有多個映像資源還可以使用Gallery來實現顯示。大致的順序為:
1. 將要顯示的圖片放到資源檔夾中
2. 定義資源ID對象數組
3. 在layout.xml中定義Gallery
4. 實現一個BaseAdapter
5. 在Activity中載入和指派對應的Adapter.
程式效果如下:
程式是我從android內建的Demo中修改來的,人懶啊!程式原始碼如下:
Activity的代碼:
package com.gloomyfish.gallery;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class HellGalleryActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(HellGalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } }); }}
ImageAdapter /ImageView介面代碼:
package com.gloomyfish.gallery;import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; public ImageAdapter(Context c) { mContext = c; TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery); mGalleryItemBackground = attr.getResourceId( R.styleable.HelloGallery_android_galleryItemBackground, 0); attr.recycle(); } public int getCount() { return mImageIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(mImageIds[position]); imageView.setLayoutParams(new Gallery.LayoutParams(200, 200)); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setBackgroundResource(mGalleryItemBackground); return imageView; }}
layout/main.xml的配置如下:
<?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content"/>