次の TestView クラスがあります。
package com.example.tview;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
public class TestView extends Activity {
FrameLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_view);
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
CustomView view = new CustomView(TestView.this);
view.setText("A");
view.setGravity(Gravity.CENTER);
view.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/led.ttf"));
view.setTextSize(400);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
}
}
CustomView クラス:
package com.example.tview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.widget.TextView;
public class CustomView extends TextView {
Paint paint;
Path path;
float x = 0;
float y = 0;
private int cYellow = Color.YELLOW;
private int[] theColors = {Color.YELLOW, Color.BLUE, Color.GREEN, Color.BLACK, Color.RED, Color.CYAN, Color.DKGRAY, Color.WHITE, Color.BLACK, Color.MAGENTA};
public CustomView(Context context) {
super(context);
paint = new Paint();
path= new Path();
paint.setAlpha(255);
paint.setColor(cYellow);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(20);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path,paint);
canvas.drawCircle(x, y, 10, paint);
}
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
path.lineTo(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
}
私のXMLレイアウト:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000"
android:layout_gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YELLOW" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GREEN" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_gravity="center" >
<FrameLayout
android:id="@+id/viewd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bd"
android:orientation="vertical" >
</FrameLayout>
</LinearLayout>
</LinearLayout>
CustomView クラスで Color 配列を設定していますが、実行時にストロークの色を設定するにはどうすればよいですか? したがって、デフォルトではそうなるはずですColor.YELLOW
が、Green
ボタンを押すと、ストロークの色はColor.GREEN
TestView クラスの更新(作業中):
package com.example.tview;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
public class TestView extends Activity {
FrameLayout layout;
Paint paint;
public int paintColor = Color.GREEN;
CustomView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_view);
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(TestView.this);
view.setText("A");
view.setGravity(Gravity.CENTER);
view.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/led.ttf"));
view.setTextSize(400);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(eHandle);
}
View.OnClickListener eHandle = new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(paintColor);
}
};
}