안드로이드 개발 질문/답변
(글 수 45,052)
자바 파일
package Test_part.main;
public class test_part extends Activity{
private DrawLine dl;
private int left = 20 , top = 20 , width = 10, height = 30;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
dl = (DrawLine)findViewById(R.id.image);
dl.left = left; dl.top = top; dl.width = width; dl.height = height;
test_draw td = new test_draw();
td.start();
}
class test_draw extends Thread{
int timer = 0;
public void run(){
while(timer != 30){
timer++;
height += 3; width += 2;
dl.height = height; dl.width = width;
dl = (DrawLine)findViewById(R.id.image);
try {
test_draw.sleep(1000);
} catch (InterruptedException e) {}
}
}
}
}
class DrawLine extends View {
Paint p = new Paint();
int left , top , width , height;
public DrawLine(Context context) {
super(context);
}
public DrawLine(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawLine(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
LinearGradient lg1 = new LinearGradient(left, top, left + width, top + height,
new int[]{Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA},
new float[]{0.2f, 0.4f, 0.6f, 0.8f, 1f}, Shader.TileMode.MIRROR);
p.setShader(lg1);
canvas.drawRect(left, top, left + width, top + height, p);
}
}
xml 파일
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<Test_part.main.DrawLine
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="150px"/>
</LinearLayout>
</ScrollView>
커스텀 위젯을 만들어서 xml에 추가시키고 그 위젯은 커스텀뷰로 선을 그리는 간단한 예제인데요
커스텀 스레드를 이용해서 30초동안 1초마다 선에 width, height 가 점점 늘어나는 예제 입니다
근데 처음에 선을 그리고 매초마다 width, height 를 점점 늘려도 선이 다시 그려지지를 않고
30초가 지나서 커스텀 스레드가 끝나면 그때서야 30초동안 커진 width, height로 한번만 다시 그리더군요
그러니까 제가 원하는건
l ll lll llll
ll lll llll
lll llll
llll
이런식으로 선이 점점 커지는게 계속 다시 그려지는걸 원하는건데
l llll
llll
llll
llll
이렇게 처음과 끝 한번만 그려집니다ㅠㅠ
매초마다 바뀐 width, height로 계속 다시 그리게 하려면 어떻게 해야하는가요ㅠㅠ?
방법을 아시면 답변 꼭좀 부탁드립니다!




음 제 경우에는 커스텀 뷰를 만들고, escarina님 처럼 xml로 속성을 준 다음에 findViewById로 커스텀 뷰를 불러온 후, 커스텀 뷰의 컨테이너 액티비티에서 다시 그리길 원할 때마다, view.invalidate() 를 호출 해 주는 형식으로 잘 사용하고 있습니다.
즉, 액티비티에서 타이머를 설정해서 30초마다 view.setSize(width, height) 이런식으로 증가하게끔 호출 후, view.invalidate() 호출을 통해 바뀐 속성을 가지고 onDraw가 호출되도록 하면 될 것 같습니다.