When I run the following code, first I see a red rectangle.
But I want to see a green rectangle first, and then when I press the button, next see a red rectangle in the same activity.
What is the solution?
MainActivity.java
public class MainActivity extends Activity {
Button button;
Draw draw;
Draw2 draw2;
RelativeLayout linearLayout;
public void onCreate(Bundle s) {
super.onCreate(s);
setContentView(R.layout.activity_main);
linearLayout = (RelativeLayout) findViewById(R.id.t);
button = (Button) findViewById(R.id.button);
draw = new Draw(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(200, 200);
draw.setLayoutParams(layoutParams);
linearLayout.addView(draw);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Draw2.class);
startActivity(intent);
}
});
draw2 = new Draw2(this);
draw2.setLayoutParams(layoutParams);
linearLayout.addView(draw2);
}
}
Draw.java
public class Draw extends View {
Paint paint;
public Draw(Context context) {
super(context);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
#Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.GREEN);
canvas.drawRect(50,30,100,120,paint);
super.onDraw(canvas);
}}
Draw2.java
public class Draw2 extends View {
Paint paint;
Draw2(Context context){
super(context);
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas){
paint.setColor(Color.RED);
canvas.drawRect(50,30,100,120,paint);
super.onDraw(canvas);
}
}
Draw2 is just a view not an activity,so Change MainActivity like this
public class MainActivity extends Activity {
Button button;
Draw draw;
Draw2 draw2;
RelativeLayout linearLayout;
ViewGroup.LayoutParams layoutParams;
public void onCreate(Bundle s) {
super.onCreate(s);
setContentView(R.layout.activity_main);
linearLayout = (RelativeLayout) findViewById(R.id.container);
button = (Button) findViewById(R.id.button1);
draw = new Draw(this);
layoutParams = new ViewGroup.LayoutParams(200, 200);
draw.setLayoutParams(layoutParams);
linearLayout.addView(draw);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
draw2 = new Draw2(getApplicationContext());
draw2.setLayoutParams(layoutParams);
linearLayout.addView(draw2);
}
});
}
}
Related
I click dice button in arenalayout.xml (this xml is displayed by 'ArenaLayout' class). But canvas doesn't draw char 2.I want to... if I click dice button then I draw char 2.
Note : After I click dice button, value of 'haveFirstDiced' variable in Arena class change to TRUE. There's condition inside of onDraw in 'Arena' class. . . if 'haveFirstDiced' variable have TRUE value then drawing char 2.
public class ArenaLayout extends Activity {
private Arena arena;
ImageButton diceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arena_layout);
diceButton = (ImageButton) findViewById(R.id.dice);
dice();
}
private void dice() {
diceButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
arena.dicing();
}
});
}
}
This is 'Arena' class :
public class Arena extends View{
private Paint p;
private Handler threadHandler = new Handler();
private Bitmap char1;
private float x = 20, y = 300;
Canvas c;
boolean haveFirstDiced = false;
public Arena(Context context, AttributeSet aSet) {
super(context, aSet);
p = new Paint();
}
public void dicing() {
new Thread(new XXX()).start();
}
#Override
synchronized public void onDraw(Canvas canvas) {
char2 = BitmapFactory.decodeResource(getResources(), R.drawable.char_2);
if(haveFirstDiced == true) {
canvas.drawBitmap(char2,x,y,null);
}
}
class XXX implements Runnable {
#Override
public void run() {
threadHandler.post(new Runnable() {
#Override
public void run() {
haveFirstDiced = true;
}
});
}
}
}
This is the most convoluted solution I've ever seen. There's no need for a thread, a runnable, or for onDraw to be synchronized. Here's what it should be:
private void dice() {
diceButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
arena.dicing();
}
});
}
public class Arena extends View{
private Paint p;
private Handler threadHandler = new Handler();
private Bitmap char2;
private float x = 20, y = 300;
Canvas c;
boolean haveFirstDiced = false;
public Arena(Context context, AttributeSet aSet) {
super(context, aSet);
p = new Paint();
char2 = BitmapFactory.decodeResource(getResources(), R.drawable.char_2);
}
public void dicing() {
haveFirstDiced = true;
invalidate();
}
#Override
public void onDraw(Canvas canvas) {
if(haveFirstDiced == true) {
canvas.drawBitmap(char2,x,y,null);
}
}
}
I want to add a textView on a relative layout dynamically, and i want to show it on a canvas. I have tried out the following code. Please help me to find out what's wrong!. Thanks in advance.
public class MainActivity2 extends Activity
{
Paint p= new Paint();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View myView= new Panel(this);
setContentView(myView);
p.setColor(Color.BLUE);
}
class Panel extends View
{
RelativeLayout rl= new RelativeLayout(getApplicationContext());
TextView tv = new TextView(getApplicationContext());
public Panel(Context context) {
super(context);
tv.setText("Helllllloo");
rl.addView(tv);
}
#Override
public void onDraw(Canvas canvas)
{
rl.draw(canvas);
canvas.drawText("helllo canvas!1!!!!!!!", 0, 100, p);
}
}
}
I solved it by adding seperate views to relative layout and set contentview as that relative layout!
i want to call the start method defined in Gameview class from the NewGame activity.basically i want to add onclicklistener and want to perform task specified inthe start() method whenever the button is clicked
activity:
public class NewGame extends Activity implements OnClickListener {
GameView gameview;
#Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//gameview=new GameView(this);
setContentView(R.layout.activity_new_game);
View startbutton=findViewById(R.id.start_button);
startbutton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_button:
gameview.start(this);
}
}
view:
public class GameView extends View {
Path circle;
Paint cPaint;
Paint tPaint;
String z;
GameView a;
int i=65,strt,arc,leftx,topy,rightx,bottomy,maxx,maxy,minx,miny;
boolean flag1,flag2,flag3;
double n1,n2;
int n,n3=180,n4,n5=90;
float f1=180,f2=90;
int width;
int height;
Random r=new Random();
RectF oval;
public GameView(Context context,AttributeSet attrs ) {
super(context,attrs);
leftx=0;
topy=60;
rightx=150;
bottomy=120;
z= String.valueOf(Character.toChars(i));
cPaint = new Paint();
cPaint.setColor(Color.RED);
strt=45;
arc=315;
n1=Math.random()*600;
Log.d("random",z);
this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// cPaint.setStrokeWidth(2);
tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
tPaint.setColor(Color.BLACK);
float scale = getResources().getDisplayMetrics().scaledDensity;
tPaint.setTextSize(20 * scale);
}
public void start(Context context)
{
if (flag2==false)
new DrawThread(this);
}
Your gameView object is currently null. If you have it in your XML layout file, you should instantiate it in onCreate with a line like this:
gameView = (GameView) findViewById(R.id.gameView);//where gameView is the id specified in your layout file (R.layout.main, or something)
If it is not in your layout file, you need to instantiate it and add it to your layout:
gameView = new GameView(this);
gameView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ViewGroup content = (ViewGroup) findViewById(android.R.id.content).getRootView();
content.addView(gameView);
now you will not get null pointer exceptions, and your gameview will fill the screen.
In the following code:
Paint tPaint = new Paint(aPaint);
tPaint.setTypeface(typeFace);
aPaint = new Paint(tPaint);
typeFace is observed set into tPaint but, typeFace does not get copied into aPaint.
Ok that´s how my code look like. See if you can find anything.
MainView.java
public class MainView extends View {
Context context;
public MainView(Context context) {
super(context);
this.context = context;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint apaint = new Paint();
apaint.setTextSize(100);
apaint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/sketchblock.ttf"));
canvas.drawText("Dusean", canvas.getWidth()/2, canvas.getHeight()/2, apaint);
Paint tpaint = new Paint(apaint);
canvas.drawText("Dusean", canvas.getWidth()/3, canvas.getHeight()/3, tpaint);
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MainView(this));
}
}
and font in the Assets folder assets/fonts/font.ttf
This is not exactly an answer but a work around.
I replaced the: aPaint = new Paint(tPaint); with: aPaint = newPaint(tPaint); and created the method:
public Paint newPaint(Paint fromPaint){
Typeface tf = fromPaint.getTypeface();
Paint rPaint = new Paint(fromPaint);
rPaint.setTypeface(tf);
return rPaint;
}
This works.
In my example when I press a button, a shape is displayed but when I press another button the previous shape gets erased.I want the shape to persist when another button is pressed. I am using the method invalidate() after every shape is displayed. Can you please give me a solution?Below is the Activity i have used.
public class StartMyDrawView extends Activity{
MyDrawView mydrawview;
public static int action=0;
Intent netIntent;
LinearLayout draw ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("StartMyDrawView", "OnCreate()");
setContentView(R.layout.designgraphic);
draw = (LinearLayout) findViewById(R.id.linearLayout1);
Button btnLine=(Button) findViewById(R.id.button1);
Button btnCircle=(Button) findViewById(R.id.button2);
Button btnRectangle=(Button) findViewById(R.id.button4);
Button btnText=(Button) findViewById(R.id.button5);
btnLine.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
action=1;
draw.invalidate();
}
});
btnCircle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
action=2;
draw.invalidate();
}
});
btnRectangle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
action=3;
draw.invalidate();
}
});
btnText.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
action=4;
draw.invalidate();
}
});
mydrawview =new MyDrawView(this);
draw.addView(mydrawview);
}
}
and My class that extends View is as follows
public class MyDrawView extends View {
Paint paint = new Paint();
int actionVal=0;
public MyDrawView(Context context) {
super(context);
}
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
if(StartMyDrawView.action==1)
canvas.drawLine(0, 0, 200, 200, paint);
if(StartMyDrawView.action==2)
canvas.drawCircle(150, 150, 50, paint);
if(StartMyDrawView.action==3)
canvas.drawRect(20, 20, 150, 150, paint);
if(StartMyDrawView.action==4)
canvas.drawText("JUST DEMO", 150, 150, paint);
}
}
I think the canvas is cleared each time onDraw is called.
One method of resolving the problem is to retain the state by doing your draw operations on a global Bitmap, and the onDraw would draw the Bitmap to the Canvas. This isn't much work, and for an example see the accepted answer here.
Android Developers topic on drawables here.