0

我有以下 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);
    }
}

自定义视图类:

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.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);
        }
    };
}
4

1 回答 1

1

您已经使用您在画布上绘制的 whihc 定义了一种绘画。按下按钮将适当的颜色设置为您的油漆:

paint.setColor(Color);

每次按下按钮时都为这条线设置正确的颜色

于 2013-08-21T01:04:52.100 回答