Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从MainActivity调用具有参数context和attributeset的方法_Java_Android_Custom View - Fatal编程技术网

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

Java 从MainActivity调用具有参数context和attributeset的方法,java,android,custom-view,Java,Android,Custom View,我有一个在屏幕上垂直显示随机字符的应用程序(如《黑客帝国》电影),我想添加按钮来更改按下时这些字符的颜色,但我无法从MainActivity调用paintTxt变量的方法setColor 这是我的密码 EffetMatrix effetMatrix = new EffetMatrix();//Error in () final Paint paintTxt = effetMatrix.paintTxt; paintTxt.setColor(Color.RED); 但编辑器显示了一个错误: Ef

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

这是我的密码

EffetMatrix effetMatrix = new EffetMatrix();//Error in ()
final Paint paintTxt = effetMatrix.paintTxt;
paintTxt.setColor(Color.RED);
但编辑器显示了一个错误:

EffetMatrix中的EffetMatrix(上下文,属性集)无法应用于()

EffetMatrix类代码

包com.esqmo.apps.effetmatrix

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.content.Context;
import android.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();
    }


}
main.xml


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

请不要再次创建自定义视图的新实例。而是使用视图的id获取对视图的引用

参见下面的代码

EffetMatrix effetMatrix = (EffetMatrix) findViewById(R.id.arrPlan);
effetMatrix.setCustomTextColor(Color.RED);
EffetMatrix
类内部创建一个名为
setCustomColor
的方法,如下所示

class EffetMatrix {
    ...

    public void setCustomTextColor(int color){
        // Set the color to the paintTxt object
        paintTxt.setColor(color);
        // invalidate the view to apply the changes
        invalidate();
    }

    ...
}
这就是如何在代码中实现它

public class MainActivity extends AppCompatActivity implements  View.OnClickListener {
    EffetMatrix effetMatrix;

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

        effetMatrix = (EffetMatrix) findViewById(R.id.arrPlan);        
        ...
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.b_b:
                effetMatrix.setCustomTextColor(Color.BLUE);
                break;
            case R.id.b_r:
                effetMatrix.setCustomTextColor(Color.RED);
                break;
            case R.id.b_ro:
                effetMatrix.setCustomTextColor(Color.MAGENTA);
                break;
        }
    }
}

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

effetMatrix.invalidate();
如果要在编辑器中显示自定义视图,则必须实现:

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

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

如果你想让这些按钮做点什么,那么你必须为主菜单中的每个按钮设置一个
onClickListener
。下面是
按钮的示例

摆脱
boutton_bleu.setOnClickListener(这个)并将其替换为

button_bleu.setOnClickListener(new OnClickListener()
    {
      public void onClick(View v)
      {
         effetMatrix.setCustomColor(Color.BLUE);
      }
    });

现在,对其他3个按钮也做同样的操作。

是不是
EffetMatrix
是一个自定义视图?不清楚
EffetMatrix
是什么,但是您对哪些参数感到困惑?您必须向我们展示
EffectMatrix
的实现,以便我们调试它。是@K Neeraj LalDo您已经在xml中有了吗?另外,你能编辑
EffetMatrix
类吗?它是预编译的吗?该应用程序在我的手机上正常运行,但我想添加可以更改字符颜色的按钮。例如当我按蓝色按钮时;所有字符都变为蓝色…@esQmo好的,只需在更改绘制颜色后调用invalidate()方法<代码>effetMatrix.invalidate()谢谢Chris,但是effetMatrix.setCustomTextColor(Color.blue);返回无法解析符号“effetMatrix”@esQmo是否错误地将“effectMatrix”输出?我相信你拼写它为“effetMatrix”而没有“c”效应矩阵或effetMatrix?我以为你错卖了it@esQmo我的错误您是对的,它是“effetMatrix”,我编辑了我的答案以适合您的方法,“setCustomColor”ok,但仍然无法在此处解析符号。我不知道我错在哪里。。。setCustomTextColor是在EffetMatrix类中定义的
public EffetMatrix(Context context) {
    this(context, null);
}

public EffetMatrix(Context context, AttributeSet attrs) {
    super(context, attrs);  
}
button_bleu.setOnClickListener(new OnClickListener()
    {
      public void onClick(View v)
      {
         effetMatrix.setCustomColor(Color.BLUE);
      }
    });