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);
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.