有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从MainActivity调用具有参数context和attributeset的方法

我有一个在屏幕上垂直显示随机字符的应用程序(就像《黑客帝国》中的电影),我想添加按钮来更改按下时这些字符的颜色,但我无法从MainActivity调用paintTxt变量的方法setColor

这是我的密码

EffetMatrix effetMatrix = new EffetMatrix();//Error in ()
final Paint paintTxt = effetMatrix.paintTxt;
paintTxt.setColor(Color.RED);

但编辑显示了一个错误:

EffetMatrix(Context, AttributSet) in EffetMatrix cannot be applied to ()

EffetMatrix类代码

包裹通讯。esqmo。应用程序。有效矩阵

import java.util.Random;

import 安卓.graphics.Bitmap;
import 安卓.graphics.Canvas;
import 安卓.graphics.Color;
import 安卓.graphics.Paint;
import 安卓.util.AttributeSet;
import 安卓.view.View;
import 安卓.content.Context;
import 安卓.widget.Button;    

/**
 * Created by esQmo on 04/10/2016.
 */
public class EffetMatrix extends View {
    private static final Random ALEATOIRE = new Random();
    private int larg, haut;
    private Canvas toile;
    private Bitmap toileBmp;
    private int taillePolice = 40;
    private int tailleColonne;
    private char[] chars = "01".toCharArray();
    private int[] posTxtParColonne;
    public Paint peindreTxt, peindreArrPlan, peindreArrPlanBmp, peindreInitArrPlan;

    public EffetMatrix(Context context, AttributeSet attrs) {
        super(context, attrs);

        peindreTxt = new Paint();
        peindreTxt.setStyle(Paint.Style.FILL);
        peindreTxt.setColor(Color.BLUE);
        peindreTxt.setTextSize(taillePolice);

        peindreArrPlan = new Paint();
        peindreArrPlan.setStyle(Paint.Style.FILL);
        peindreArrPlan.setColor(Color.BLACK);
        peindreArrPlan.setAlpha(5);

        peindreArrPlanBmp = new Paint();
        peindreArrPlanBmp.setColor(Color.BLACK);

        peindreInitArrPlan = new Paint();
        peindreInitArrPlan.setColor(Color.BLACK);
        peindreInitArrPlan.setAlpha(255);
        peindreInitArrPlan.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onSizeChanged(int l, int h, int ancl, int anch) {
        super.onSizeChanged(l, h, ancl, anch);

        larg = l;
        haut = h;

        toileBmp = Bitmap.createBitmap(larg, haut, Bitmap.Config.ARGB_8888);
        toile = new Canvas(toileBmp);
        toile.drawRect(0, 0, larg, haut, peindreInitArrPlan);
        tailleColonne = larg / taillePolice;

        posTxtParColonne = new int[tailleColonne + 1];

        for (int x = 0; x < tailleColonne; x++) {
            posTxtParColonne[x] = ALEATOIRE.nextInt(larg / 2) + 1;
        }

    }

    private void dessineTexte() {
        for (int i = 0; i < posTxtParColonne.length; i++) {
            toile.drawText("" + chars[ALEATOIRE.nextInt(chars.length)], i * taillePolice,
                    posTxtParColonne[i] * taillePolice, peindreTxt);
            if (posTxtParColonne[i] * taillePolice > larg && Math.random() > 0.980) {
                posTxtParColonne[i] = 0;
            }
            posTxtParColonne[i]++;
        }
    }

    private void dessineToile() {
        toile.drawRect(0, 0, larg, haut, peindreArrPlan);
        dessineTexte();
    }

    @Override
    protected void onDraw(Canvas toile) {
        super.onDraw(toile);
        toile.drawBitmap(toileBmp, 0, 0, peindreArrPlanBmp);
        dessineToile();
        invalidate();
    }
    public void setCustomColor(int color){
        peindreTxt.setColor(color);
        invalidate();
    }


}

注:变量peindreText=PAINTEXT

主要活动代码:

package com.esqmo.apps.effetmatrix;

import 安卓.content.DialogInterface;
import 安卓.content.Intent;
import 安卓.graphics.Color;
import 安卓.graphics.Paint;
import 安卓.media.effect.Effect;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button boutton_vert = (Button) findViewById(R.id.b_v);
        final Button boutton_bleu = (Button) findViewById(R.id.b_b);
        final Button boutton_rouge = (Button) findViewById(R.id.b_r);
        final Button boutton_rose = (Button) findViewById(R.id.b_ro);

        boutton_bleu.setOnClickListener(this);
        boutton_vert.setOnClickListener(this);
        boutton_rouge.setOnClickListener(this);
        boutton_rose.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

    }


    public void passerVert(View v) {

    }

    public void passerRouge(View v) {

    }

    public void passerRose(View v) {

    }

    public void passerBleu(View v) {


    }
}

梅因。xml

<com.esqmo.apps.effetmatrix.EffetMatrix
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:id="@+id/arrPlan"/>

<LinearLayout
    安卓:orientation="horizontal"
    安卓:layout_width="match_parent"
    安卓:layout_height="50dp"
    安卓:layout_alignParentBottom="true"
    安卓:layout_alignParentStart="true"
    安卓:background="#040404"
    安卓:id="@+id/linearLayout">

    <Button
        安卓:layout_width="70dp"
        安卓:layout_height="match_parent"
        安卓:text="@string/button_vert"
        安卓:onClick="passerVert"
        安卓:id="@+id/b_v"
        安卓:textAppearance="@安卓:color/holo_blue_dark" />

    <Button
        安卓:layout_width="70dp"
        安卓:layout_height="match_parent"
        安卓:text="@string/button_bleu"
        安卓:onClick="passerBleu"
        安卓:id="@+id/b_b"/>

    <Button
        安卓:layout_width="70dp"
        安卓:layout_height="match_parent"
        安卓:text="@string/button_rouge"
        安卓:onClick="passerRouge"
        安卓:id="@+id/b_r" />

    <Button
        安卓:layout_width="70dp"
        安卓:layout_height="match_parent"
        安卓:text="@string/button_rouge"
        安卓:onClick="passerRose"
        安卓:id="@+id/b_ro" />



</LinearLayout>

附言:我是编程高手。 对不起我的英语


共 (1) 个答案

  1. # 1 楼答案

    更改绘制颜色后,必须使自定义视图无效

    effetMatrix.invalidate();
    

    如果要在编辑器中显示自定义视图,则必须实现:

    public EffetMatrix(Context context) {
        this(context, null);
    }
    
    public EffetMatrix(Context context, AttributeSet attrs) {
        super(context, attrs);  
    }