도래울

안드로이드 커스텀뷰 예제 본문

개발/Android

안드로이드 커스텀뷰 예제

도래울 2016. 2. 5. 11:43

Main.java

package com.andr;

import android.app.Activity;
import android.os.Bundle;

public class main extends Activity{
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(new GraphicsView(this));
 }
}


GraphicsView.java

 

package com.andr;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.view.View;

public class GraphicsView  extends View{

 private final Path path;
 private final Paint pPaint;
 private final Paint tPaint;
 private final String words;
 
 public GraphicsView(Context context) {
  super(context);
  path = new Path();
  path.addRect(50, 50,200,80,Direction.CW);
  pPaint=new Paint();
  pPaint.setStyle(Paint.Style.STROKE);
  pPaint.setColor(Color.LTGRAY);
  pPaint.setStrokeWidth(2);
  words="It's Custom View Test";
  tPaint= new Paint();
  tPaint.setColor(Color.WHITE);
  tPaint.setTextSize(15f);
 }
 public void onDraw(Canvas canvas) {

  canvas.drawPath(path, pPaint);
  canvas.drawTextOnPath(words, path, 0, 20, tPaint);
 }
}


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">

 <com.andr.main.GraphicsView android:id="@+id/graphics"
  android:layout_width="fill_parent" android:layout_height="fill_parent"/>

</LinearLayout> 
 

 

Comments