1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.graphics; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.graphics.*; 24 import android.os.Bundle; 25 import android.view.KeyEvent; 26 import android.view.*; 27 28 import java.io.InputStream; 29 import java.io.ByteArrayOutputStream; 30 31 public class AlphaBitmap extends GraphicsActivity { 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(new SampleView(this)); 37 } 38 39 private static class SampleView extends View { 40 private Bitmap mBitmap; 41 private Bitmap mBitmap2; 42 private Bitmap mBitmap3; 43 private Shader mShader; 44 45 private static void drawIntoBitmap(Bitmap bm) { 46 float x = bm.getWidth(); 47 float y = bm.getHeight(); 48 Canvas c = new Canvas(bm); 49 Paint p = new Paint(); 50 p.setAntiAlias(true); 51 52 p.setAlpha(0x80); 53 c.drawCircle(x/2, y/2, x/2, p); 54 55 p.setAlpha(0x30); 56 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); 57 p.setTextSize(60); 58 p.setTextAlign(Paint.Align.CENTER); 59 Paint.FontMetrics fm = p.getFontMetrics(); 60 c.drawText("Alpha", x/2, (y-fm.ascent)/2, p); 61 } 62 63 public SampleView(Context context) { 64 super(context); 65 setFocusable(true); 66 67 InputStream is = context.getResources().openRawResource(R.drawable.app_sample_code); 68 mBitmap = BitmapFactory.decodeStream(is); 69 mBitmap2 = mBitmap.extractAlpha(); 70 mBitmap3 = Bitmap.createBitmap(200, 200, Bitmap.Config.ALPHA_8); 71 drawIntoBitmap(mBitmap3); 72 73 mShader = new LinearGradient(0, 0, 100, 70, new int[] { 74 Color.RED, Color.GREEN, Color.BLUE }, 75 null, Shader.TileMode.MIRROR); 76 } 77 78 @Override protected void onDraw(Canvas canvas) { 79 canvas.drawColor(Color.WHITE); 80 81 Paint p = new Paint(); 82 float y = 10; 83 84 p.setColor(Color.RED); 85 canvas.drawBitmap(mBitmap, 10, y, p); 86 y += mBitmap.getHeight() + 10; 87 canvas.drawBitmap(mBitmap2, 10, y, p); 88 y += mBitmap2.getHeight() + 10; 89 p.setShader(mShader); 90 canvas.drawBitmap(mBitmap3, 10, y, p); 91 } 92 } 93 } 94 95