我有问题。我想在片段中画圆圈。当我触摸一个圆圈时,我想重新启动。所以所有被触摸的圈子都应该成为一种新的颜色。 问题是onDraw只启动一次。我试图使布局无效,但它没有帮助。 有谁知道如何解决这个问题?
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@SuppressLint("ClickableViewAccessibility")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, Bundle savedInstanceState) {
final View fraglayoutv3 = inflater.inflate(R.layout.frag3_layout,null);
final LinearLayout touchLayout = (LinearLayout) fraglayoutv3.findViewById(R.id.touchLayout);
touchLayout.addView(new Leds(getActivity()));
coordinates = "Coordinates: ";
touchLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
int x=(int)event.getX();
int y=(int)event.getY();
int xy = 0;
int d = 24;
xPos=0;
yPos=0;
Log.v("TAG", x+" | "+y);
for(int i=0;i<32;i++)
{
if(x>xy && x<xy+d){
xPos = i;
}
xy+=d;
}
xy=0;
for(int i=0;i<80;i++){
if(y>xy && y<xy+d){
yPos = i;
}
xy+=d;
}
coordinates += (xPos + ";" +yPos +";");
xArray[nr] = x;
yArray[nr] = y;
nr++;
data = true;
}
Log.v("Tag",coordinates);
return true;
}
});
return fraglayoutv3;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private class Leds extends View{
Paint paint = new Paint();
Paint paintText = new Paint();
Paint paintLeds = new Paint();
public Leds(Context context){
super(context);
}
@Override
public void onDraw(Canvas canvas){
paint.setColor(Color.BLACK);
paintText.setColor(Color.GREEN);
paintLeds.setColor(Color.YELLOW);
//int a=20; // Offset x-Achse
//int b=50; // Offset y-Achse
int a =12;
int b=12;
int nummer = 0;
for(int j=0;j<80;j++){ // 80 Zeilen lang
for(int i=0;i<32;i++){ // 32 Spalten
canvas.drawCircle(a,b,12,paint);
canvas.drawText(i+"|"+j,a-10,b,paintText);
nummer++;
a+=24;
}
//a = 20;
b+=24;
a=12;
}
Log.v("TAG", "Touch");
for(int i=0;i<nr;i++){
canvas.drawCircle(xArray[i],yArray[i],12,paintLeds);
}
}
}
}