• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context;
22 import android.graphics.*;
23 import android.os.Bundle;
24 import android.view.*;
25 
26 import java.io.InputStream;
27 
28 public class AlphaBitmap extends GraphicsActivity {
29 
30     @Override
onCreate(Bundle savedInstanceState)31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         setContentView(new SampleView(this));
34     }
35 
36     private static class SampleView extends View {
37         private Bitmap mBitmap;
38         private Bitmap mBitmap2;
39         private Bitmap mBitmap3;
40         private Shader mShader;
41 
drawIntoBitmap(Bitmap bm)42         private static void drawIntoBitmap(Bitmap bm) {
43             float x = bm.getWidth();
44             float y = bm.getHeight();
45             Canvas c = new Canvas(bm);
46             Paint p = new Paint();
47             p.setAntiAlias(true);
48 
49             p.setAlpha(0x80);
50             c.drawCircle(x/2, y/2, x/2, p);
51 
52             p.setAlpha(0x30);
53             p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
54             p.setTextSize(60);
55             p.setTextAlign(Paint.Align.CENTER);
56             Paint.FontMetrics fm = p.getFontMetrics();
57             c.drawText("Alpha", x/2, (y-fm.ascent)/2, p);
58         }
59 
SampleView(Context context)60         public SampleView(Context context) {
61             super(context);
62             setFocusable(true);
63 
64             InputStream is = context.getResources().openRawResource(R.drawable.app_sample_code);
65             mBitmap = BitmapFactory.decodeStream(is);
66             mBitmap2 = mBitmap.extractAlpha();
67             mBitmap3 = Bitmap.createBitmap(200, 200, Bitmap.Config.ALPHA_8);
68             drawIntoBitmap(mBitmap3);
69 
70             mShader = new LinearGradient(0, 0, 100, 70, new int[] {
71                                          Color.RED, Color.GREEN, Color.BLUE },
72                                          null, Shader.TileMode.MIRROR);
73         }
74 
onDraw(Canvas canvas)75         @Override protected void onDraw(Canvas canvas) {
76             canvas.drawColor(Color.WHITE);
77 
78             Paint p = new Paint();
79             float y = 10;
80 
81             p.setColor(Color.RED);
82             canvas.drawBitmap(mBitmap, 10, y, p);
83             y += mBitmap.getHeight() + 10;
84             canvas.drawBitmap(mBitmap2, 10, y, p);
85             y += mBitmap2.getHeight() + 10;
86             p.setShader(mShader);
87             canvas.drawBitmap(mBitmap3, 10, y, p);
88         }
89     }
90 }
91 
92