1. Draw on the canvas (draw with a canvas)
Android provides a custom rendering API to draw various graphics, text, and image objects on the canvas.
In fact, all the canvas objects will be drawn to the bitmap object at the bottom layer of the canvas to create a canvas
The object statement is as follows:
Bitmap B = bitmap. createbitmap (100,100, bitmap. config. argb_8888 );
Canvas c = new canvas (B );
First, create a 32-Bit Bitmap object of the argb type, and then create a canvas on this bitmap object)
Object.
View. Draw (C );
Fileoutputstreamfos = new fileoutputstream (new file (app_file_path + "/canvastoimage.png "));
Bitmap. Compress (bitmap. compressformat. JPEG, 100, FOS );
Output the content in the view to the image and compress the image to the JPG in the specified format. Fos is the output stream of the file.
To display the canvas content on an android object, reload the ondraw () method of the view.
The paintcomponent () method to override jcomponent is a bit similar, so that you can obtain the drawing
To draw custom objects. The code for canvas rendering and display using a complete override view is as follows:
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); } }}
The program effect is as follows:
2. Use a drawables object)
Android also provides a relatively simple and convenient API for reading and writing and displaying image files, that is, loading image objects as resources,
Use the imageview API to display images. If multiple image resources exist, you can use gallery to display images. The general order is:
1. Place the image to be displayed in the resource folder.
2. Define an array of resource ID objects
3. Define Gallery in layout. xml
4. Implement a baseadapter
5. Load and assign the corresponding adapter in the activity.
The program effect is as follows:
The program was modified from the demo provided by Android. It's lazy!The program source code is as follows:
Activity Code:
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 interface code:
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 is configured as follows:
<?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"/>