android開發之建立BMP位元影像

來源:互聯網
上載者:User

大體思路,首先Bitmap.createBitmap建立位元影像,然後自訂函數drawIntoBitmap,最後通過onDraw建立畫布來實現繪製的位元影像

package com.example;

import java.io.InputStream;

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.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.os.Bundle;
import android.view.View;

public class AlphaBitmapActivity extends Activity {
 public Bitmap mBitmap1, mBitmap2, mBitmap3;
 public Shader mShader;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
       }
    public class SampleView extends View {
     public SampleView(Context context) {
      super(context);
      setFocusable(true);   //控制項允許擷取焦點
      InputStream is = context.getResources().openRawResource(R.drawable.sample);
      mBitmap1 = Bitmap.createBitmap(200, 200, Bitmap.Config.ALPHA_8);
      drawIntoBitmap(mBitmap3、1);
      /*產生 LinearGradient對象
          new LinearGradien(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR);
       參數一:為漸層起點座標的x軸位置,
       參數二 :為漸層起點座標的y軸位置,
       參數三 :為漸層終點座標的x軸位置,
       參數四 :為漸層終點座標的y軸位置,
       參數五 :平鋪方式,這裡設定為鏡像
         */
      mShader = new LinearGradient(0, 0, 300, 500, new int[] {
        Color.RED, Color.GREEN, Color.BLUE}, null, Shader.TileMode.MIRROR);
     }

     /*繪製位元影像*/
     public void drawIntoBitmap(Bitmap bm) {
      float x = bm.getWidth();
      float y = bm.getHeight();
      Canvas c = new Canvas(bm);
      Paint p = new Paint();
      p.setAntiAlias(true);
      p.setAlpha(0x80);
      c.drawCircle(x/2, y/2, x/2, p);

      p.setAlpha(0x80);
      p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
      p.setTextSize(60);
      p.setTextAlign(Paint.Align.CENTER);
     Paint.FontMetrics fm = p.getFontMetrics();
      c.drawText("Alpha", x/2, y/2-fm.ascent/2, p);   //"Alpha"字串在繪製的圓形中的文字
     }
    
     public void onDraw(Canvas canvas) {
         canvas.drawColor(Color.WHITE);                //畫布底色設為白色
         Paint p = new Paint();
         p.setColor(Color.RED);                                 //設定文字顏色
         p.setTextSize(36);                                         //設定文字大小
         p.setShader(mShader);                                //設定映像陰影製作效果
         canvas.drawBitmap(mBitmap1, bw, bh, p);
         /*得到畫布寬度高度*/
         float cw= canvas.getWidth();
         float ch = canvas.getHeight();
         canvas.drawText("寬度 "+cw+" 高度 "+ ch, 50, 50, p);
       }
    }

}

結果

另外說明的是 注意canvas.drawText()和canvas.drawBitmap()

drawText(String text, float x, float y, Paint paint) 中x,y是從字串的左下角開始,y是字串的baseline

drawBitmap(Bitmap bitmap, float left, float top, Paint paint)中x,y是產生位元影像的左上方的點到畫布左邊和上邊的距離

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.