ã«ã¹ã¿ã ãã¥ã¼ã®å®è£
Viewã«å³å½¢ã¨ããæç»ãããã¦ãViewãç¶æ¿ããã«ã¹ã¿ã ãã¥ã¼ãå®è£
ãã¦ã¿ã¾ããã
ããæ¹ã¯ç°¡åã§ãViewãç¶æ¿ããã¯ã©ã¹ãä½æå¾ãã¬ã¤ã¢ã¦ããã¡ã¤ã«ã«ããã±ã¼ã¸åãå«ãã¯ã©ã¹åãXMLã¿ã°ã¨ãã¦è¨å®ããã ãã
ï¼ï¼Viewãç¶æ¿ããã¯ã©ã¹ãä½æ
ChartView.java
package sample.controll; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; import android.view.View; public class ChartView extends View { private final String TAG = "ChartViewActivity"; private boolean mPiDraw = false; private boolean mRectDraw = false; public ChartView(Context context) { super(context); // TODO Auto-generated constructor stub setFocusable(true); } public ChartView(Context context, AttributeSet attrs) { super(context, attrs); setFocusable(true); } public ChartView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setFocusable(true); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub int width = getWidth(); int height = getHeight(); if (mPiDraw) { Paint paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); paint.setColor(getResources().getColor(R.color.aqua)); canvas.drawCircle(width/2, height/2, 100, paint); Log.d(TAG, "Circle Draw"); } else if (mRectDraw) { } Log.d(TAG, "width : " +width + " height : " + height); } public void drawPiChart() { mPiDraw = true; mRectDraw = false; invalidate(); } public void drawRectChart() { mPiDraw = false; mRectDraw = true; invalidate(); } }
â»ç¶æ¿ããã¯ã©ã¹ã§ã¯ã親ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ããã¹ã¦è¨è¿°ãã¦ãããªãã¨ãä½æ ãã¨ã©ã¼ãçºçããã®ã§æ³¨æã
ï¼ï¼ã¬ã¤ã¢ã¦ãXMLãã¡ã¤ã«ã®è¨å®
chartview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingBottom="10dip" android:orientation="vertical" android:layout_height="fill_parent" android:id="@+id/Relativ" android:paddingTop="5dip"> <sample.controll.ChartView android:layout_marginBottom="50dip" android:layout_marginTop="30dip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/ChartView" android:background="@color/lightBlack"> </sample.controll.ChartView> <Button android:id="@+id/Button01" android:layout_height="wrap_content" android:text="@string/finish" android:textSize="20sp" android:layout_width="wrap_content" android:layout_alignParentBottom="true"> </Button> <Button android:layout_height="wrap_content" android:textSize="20sp" android:layout_alignParentBottom="true" android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_toRightOf="@+id/Button01" android:text="@string/Draw" android:gravity="center"> </Button> <TextView android:id="@+id/TextView01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:textSize="20sp" android:gravity="center_horizontal" android:text="@string/ChartTitle" android:textColor="@color/aqua" android:textStyle="bold"> </TextView> </RelativeLayout>
ï¼ï¼ã¢ã¯ãã£ããã£ã¯ã©ã¹
ChartActivity.java
package sample.controll; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.util.Log; public class ChartActivity extends Activity implements OnClickListener { public static String TAG = "ChartActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chartview); View btn1 = this.findViewById(R.id.Button01); btn1.setOnClickListener(this); View btn2 = this.findViewById(R.id.Button02); btn2.setOnClickListener(this); Log.d(TAG, "setButton Button1 Button2"); Log.d(TAG, "Make"); } public void onClick(View v) { switch (v.getId()) { case R.id.Button01: finish(); break; case R.id.Button02: ChartView view = (ChartView)this.findViewById(R.id.ChartView); view.drawPiChart(); break; default: break; } } }