Android 입문 의 그림 상세 설명

앞에서 자주 사용 하 는 컨트롤 러 를 많이 소 개 했 으 니 휴대 전화 개발 에 자주 사용 되 는 그림 에 대해 논의 해 보 자.Android 의 그림 을 파악 하려 면 먼저 다음 과 같은 그래 픽 인 터 페 이 스 를 사용 해 야 합 니 다.
1.Bitmap 는 자원/파일 에서 나 올 수도 있 고 프로그램 에서 만 들 수도 있 으 며 실제 기능 은 그림 의 저장 공간 에 해당 합 니 다.
2.Canvas,Bitmap 와 밀접 한 관 계 를 가지 고 Bitmap 를 내용 에 비유 하면 Canvas 는 여러 가지 방법 으로 Bitamp 를 조작 하 는 플랫폼 을 제공 합 니 다.
3.Paint 는 Canvas 와 밀접 한 관 계 를 가지 고'화판'의 브러시 도구 이 며 View 컨트롤 의 스타일 을 설정 하 는 데 도 사 용 됩 니 다.
4.Drawable,앞의 세 사람 이 보이 지 않 게 메모리 에 그림 을 그린다 면 Drawable 은 앞의 세 사람의 그림 결 과 를 나타 내 는 인터페이스 입 니 다.Drawable 여러 개의 하위 클래스,예 를 들 어 비트 맵(BitmapDrawable),도형(ShapeDrawable),그래 픽(LayerDrawable)등 입 니 다.
본 고 는 ImageView 에 그림 을 그 리 는 방법 과 Button(View 를 계승 하 는 컨트롤)에 사용자 정의 그림 을 직접 그 리 는 방법 을 설명 한다.다음 그림 에서 보 듯 이:

자원 그림 직접 그리 기:
 
ImageView 에 그림 그리 기 및 글자 그리 기:

컨트롤 배경 에 직접 그림 그리 기:
main.xml 의 원본 코드 는 다음 과 같 습 니 다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<Button android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="44px" android:text="      "></Button>
<Button android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_height="44px" android:text="         "></Button>
<Button android:id="@+id/Button03" android:layout_height="44px" android:layout_width="fill_parent" android:text="      "></Button>
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

</LinearLayout>

자바 프로그램의 원본 코드 는 다음 과 같 습 니 다.

package com.testDraw;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class testDraw extends Activity {
  
 ImageView iv;
 Button btn1,btn2,btn3,btn4;
 Resources r;
 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    iv=(ImageView)this.findViewById(R.id.ImageView01);
    btn1=(Button)this.findViewById(R.id.Button01);
    btn2=(Button)this.findViewById(R.id.Button02);
    btn3=(Button)this.findViewById(R.id.Button03);

    btn1.setOnClickListener(new ClickEvent());
    btn2.setOnClickListener(new ClickEvent());
    btn3.setOnClickListener(new ClickEvent());
    
    r = this.getResources();
  }
 class ClickEvent implements View.OnClickListener {

 public void onClick(View v) {
  if(v==btn1)//      
  {//    
  //iv.setBackgroundResource(R.drawable.icon);//      
  Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);//      
  iv.setImageBitmap(bmp);
  }
  else if(v==btn2)//         
  {
     Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);//  ,     bmp  
     Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );
     
     Canvas canvasTemp = new Canvas( newb );
     canvasTemp.drawColor(Color.TRANSPARENT);
     
     Paint p = new Paint();
     String familyName ="  ";
     Typeface font = Typeface.create(familyName,Typeface.BOLD);
     p.setColor(Color.RED);
     p.setTypeface(font);
     p.setTextSize(22);
     canvasTemp.drawText("  。。。",50,50,p);
     canvasTemp.drawBitmap(bmp, 50, 50, p);//  
     iv.setImageBitmap(newb);
  }
  else if(v==btn3)//   Button   
  {
  Bitmap newb = Bitmap.createBitmap( btn3.getWidth(), btn3.getHeight(), Config.ARGB_8888 );
  Canvas canvasTemp = new Canvas( newb );
    canvasTemp.drawColor(Color.WHITE);
    Paint p = new Paint();
  String familyName = "  ";
  Typeface font = Typeface.create(familyName, Typeface.BOLD);
  p.setColor(Color.RED);
  p.setTypeface(font);
  p.setTextSize(20);
  canvasTemp.drawText("  。。。", 30, 30, p);
  Drawable drawable = new BitmapDrawable(newb);
  btn3.setBackgroundDrawable(drawable);
  }
 }
 }
}

좋은 웹페이지 즐겨찾기