我创建了一个扩展View的java类,在主要活动中我初始化了更多的CostumDrawableView,但我想使用相同的CostumDrawableView(例如:greenDrawableView)两次或更多次,它可能吗? 我该怎么办?
如果我写" greenLayout2.addView(greenDrawableView);"活动没有打开和崩溃。 感谢
package com.example.prova.shaperectprove;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.text.Layout;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.RelativeLayout;
import java.util.ArrayList;
public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;
int x,y,width,height,drawColor;
public CustomDrawableView(Context context,int drawColor,int left,int top,int right,int bottom) {
super(context);
mDrawable = new ShapeDrawable(new RectShape());
mDrawable.getPaint().setColor(drawColor);
mDrawable.setBounds(left,top, right, bottom); //limiti rispetto all'activity
//left,top,right,bottom
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
protected void changeColor(int newColor) {
drawColor = newColor;
}
protected int getLenght()
{
return width-x; //right-left
}
protected int getHight()
{
return height-y; //bottom-top
}
protected int mygetLeft()
{
return x;
}
protected int mygetTop()
{
return y;
}
protected int mygetRight()
{
return width;
}
protected int mygetBottom()
{
return height;
}
protected void changeSize(int x, int y,int width, int height){
mDrawable.setBounds(x, y, width, height); //limiti rispetto all'activity
invalidate();
}
}

package com.example.prova.shaperectprove;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private LinearLayout parent,myfirstLayout,mysecondLayout;
private LinearLayout redLayout,greenLayout,blueLayout,redLayout2,greenLayout2;
// Resources res = getBaseContext().getResources();
ShapeDrawable myshape;
CustomDrawableView greenDrawableView,greenDrawableView2;
CustomDrawableView redDrawableView,redDrawableView2;
CustomDrawableView blueDrawableView;
Canvas mycanvas = new Canvas();
final private static String str = "Green";
final private static int idG = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
parent = new LinearLayout(getApplicationContext());
parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
parent.setOrientation(LinearLayout.VERTICAL);
myfirstLayout = new LinearLayout(getApplicationContext());
myfirstLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100));
myfirstLayout.setOrientation(LinearLayout.HORIZONTAL);
mysecondLayout = new LinearLayout(getApplicationContext());
mysecondLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100));
mysecondLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams myLLP = new LinearLayout.LayoutParams(100, 100);
LinearLayout.LayoutParams myLLP2 = new LinearLayout.LayoutParams(200, 100);
LinearLayout.LayoutParams myLLP3 = new LinearLayout.LayoutParams(300, 100);
redLayout = new LinearLayout(getApplicationContext());
redLayout.setLayoutParams(myLLP2);
redLayout.setOrientation(LinearLayout.HORIZONTAL);
greenLayout = new LinearLayout(getApplicationContext());
greenLayout.setLayoutParams(myLLP3);
greenLayout.setOrientation(LinearLayout.HORIZONTAL);
blueLayout = new LinearLayout(getApplicationContext());
blueLayout.setLayoutParams(myLLP);
blueLayout.setOrientation(LinearLayout.HORIZONTAL);
redLayout2 = new LinearLayout(getApplicationContext());
redLayout2.setLayoutParams(myLLP2);
redLayout2.setOrientation(LinearLayout.HORIZONTAL);
greenLayout2 = new LinearLayout(getApplicationContext());
greenLayout2.setLayoutParams(myLLP3);
greenLayout2.setOrientation(LinearLayout.HORIZONTAL);
blueDrawableView = new CustomDrawableView(this,0xFF68AEF0,10,10,100,80);
greenDrawableView = new CustomDrawableView(this,0xFF1A9316,0,10,300,80);
redDrawableView = new CustomDrawableView(this,0xFFC12727,0, 10, 200, 80);
greenDrawableView2 = new CustomDrawableView(this,0xFF1A9316,10,10,300,80);
redDrawableView2 = new CustomDrawableView(this,0xFFC12727,0, 10, 200, 80);
greenDrawableView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
MyDialogGreen dialogGreen = new MyDialogGreen();
dialogGreen.show(fm, str);
}
});
redDrawableView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
MyDialogRed dialogRed = new MyDialogRed();
dialogRed.show(fm, str);
}
});
blueDrawableView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
MyDialogBlue dialogBlue = new MyDialogBlue();
dialogBlue.show(fm, str);
}
});
greenLayout.addView(greenDrawableView);
redLayout.addView(redDrawableView);
blueLayout.addView(blueDrawableView);
myfirstLayout.addView(blueLayout);
myfirstLayout.addView(greenLayout);
myfirstLayout.addView(redLayout);
parent.addView(myfirstLayout);
greenLayout2.addView(greenDrawableView2);
redLayout2.addView(redDrawableView2);
mysecondLayout.addView(greenLayout2);
mysecondLayout.addView(redLayout2);
parent.addView(mysecondLayout);
setContentView(parent);
}
}