반응형
그래픽 그리기가 가능한 요소들은 드로어블 객체로 만들어 그릴 수 있다
그래픽을 그리는 하나의 단위를 그리기 객체로 만들어 두면 각각의 그래픽 그리기 작업을 독립적인 객체로 나누어 관리할 수 있는 장점이 있다
이 객체에 애니메이션을 적용할 수도 있다
셰이프 드로어블은 도형으로 정의된 Shape 객체에 담을 수 있으며, 이를 이용해서 메모리에 만들어진 그래픽 정보를 관리할 수 있도록 한다
/app/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="color01">#FF000000</color>
<color name="color02">#FF888888</color>
<color name="color03">#FF333333</color>
</resources>
CustomViewDrawable.java
package com.example.a67_sample_customviewdrawable;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class CustomViewDrawable extends View {
private ShapeDrawable upperDrawable;
private ShapeDrawable lowerDrawable;
public CustomViewDrawable(Context context) {
super(context);
init(context);
}
public CustomViewDrawable(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init(context);
}
private void init(Context context) {
// 윈도우 매니저를 이용해 뷰의 폭과 높이 확인
// 뷰가 채워지는 화면의 크기를 알아오기 위해, 시스템 서비스 객체인 윈도우 매니저를 참조
// 색상 정보는 /app/res/values/ 폴더 밑에 colors.xml에 저장
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Resources curRes = getResources();
int blackColor = curRes.getColor(R.color.color01);
int grayColor = curRes.getColor(R.color.color02);
int darkGreyColor = curRes.getColor(R.color.color03);
upperDrawable = new ShapeDrawable();
RectShape rectShape = new RectShape();
rectShape.resize(width, height * 2 / 3);
upperDrawable.setShape(rectShape);
upperDrawable.setBounds(0, 0, width, height * 2 / 3);
LinearGradient gradient = new LinearGradient(0, 0, 0, height * 2 / 3, grayColor, blackColor, Shader.TileMode.CLAMP);
Paint paint = upperDrawable.getPaint();
paint.setShader(gradient);
lowerDrawable = new ShapeDrawable();
RectShape rectShape2 = new RectShape();
rectShape2.resize(width, height * 1 / 3);
lowerDrawable.setShape(rectShape2);
lowerDrawable.setBounds(0, height * 2 / 3, width, height);
LinearGradient gradient2 = new LinearGradient(0, 0, 0, height * 1 / 3, blackColor, darkGreyColor, Shader.TileMode.CLAMP);
Paint paint2 = lowerDrawable.getPaint();
paint2.setShader(gradient2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
upperDrawable.draw(canvas);
lowerDrawable.draw(canvas);
}
}
MainActivity
package com.example.a67_sample_customviewdrawable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
CustomViewDrawable customViewDrawable = new CustomViewDrawable(this);
setContentView(customViewDrawable);
}
}
반응형
'안드로이드' 카테고리의 다른 글
BtimapFactory 클래스 (0) | 2021.11.02 |
---|---|
비트맵 이미지 사용 (0) | 2021.11.02 |
뷰에 그래픽 그리기 (0) | 2021.11.01 |
내용 제공자 사용하기, 연락처 조회 (0) | 2021.11.01 |
내용 제공자 사용하기, 앨범 조회 (0) | 2021.11.01 |