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.
Related
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);
}
});
}
}
I am trying to draw some text above the thumb of the SeekBar like in the image above. The idea is that when you move the thumb along the SeekBar text is displayed above the thumb. I am able to draw text inside the thumb but when I try and draw it outside the thumb it doesn't work. Below is my code:
public class MainActivity extends Activity {
SeekBar mybar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mybar = (SeekBar) findViewById(R.id.seekBar1);
mybar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// add here your implementation
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// add here your implementation
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int value = seekBar.getProgress();
String valueString = value + "";
Paint paint = new Paint();
paint.setColor(Color.BLACK);
Bitmap b = Bitmap.createBitmap(600, 600,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
canvas.drawText(valueString, 100, 100, paint);
seekBar.draw(canvas);
}
});
}
}
Do I have to override the onDraw method and do it that way instead?
I'm trying to follow instructions from Android developers site but I must be doing something wrong.
I tried to create custom ImageView and draw 2 bitmaps onto it.
Activity
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
LinearLayout left = (LinearLayout) findViewById(R.id.container);
ModuleImageView iv = new ModuleImageView(this);
left.addView(iv);
iv.invalidate();
}
}
ImageView
public class ModuleImageView extends ImageView{
public ModuleImageView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
Bitmap b1 = BitmapFactory.decodeResource(getResources(), R.drawable.main_engine);
Bitmap b2 = BitmapFactory.decodeResource(getResources(), R.drawable.energy);
canvas.drawBitmap(b1, 0, 0, null);
canvas.drawBitmap(b2, 5, 5, null);
super.onDraw(canvas);
}
}
There is nothing showing up on the screen and it's probably because the onDraw method is never executed.
Use LayerDrawable instead. Construct it and pass to a normal ImageView
So I have this class which extends an activity. But I want to draw something on the screen, so I need to make a canvas. However I can't extends View, because it's an activity allready. What should I do?
My activity has the onClick method which I use to do some stuff, but what I wanna do is draw a simple image when I call the onClick method as well.
Thanks.
public class Stuff extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
(...)
}
#Override
public void onClick(View arg0) {
(...)
}
STEP 1: create a class by extends View as:
public class DrawView extends View {
public float currentX=40;
public float currentY=50;
public DrawView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint=new Paint();
paint.setColor(Color.RED);
canvas.drawCircle(currentX, currentY, 25, paint);
}
}
STEP 2: In Your Stuff Activity :
public class Stuff extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout root=(LinearLayout) findViewById(R.id.root);
(...)
}
#Override
public void onClick(View arg0) {
//DRAW YOUR VIEW ON BUTTON CLICK
final DrawView drawView=new DrawView(this);
drawView.setMinimumWidth(300);
drawView.setMinimumHeight(500);
drawView.currentX=200;
drawView.currentY=200;
drawView.invalidate();
root.addView(drawView);
(...)
}
STEP 3: Your Activity main.xml as :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#99FFCC"
android:id="#+id/root">
</LinearLayout>
and finally try to search on google before asking question here.thanks
You can declare an inner class within ur activity for ex refer this code:
public class GraphicsTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicsTestView(this));
}
private static class GraphicsTestView extends View
{
private ShapeDrawable mDrawable =
new ShapeDrawable();
/* Drawable's are objects which can be drawn on a Canvas
ShapeDrawable is used to draw primitive shapes such as:
ArcShape, OvalShape, PathShape, RectShape, RoundRectShape
A Canvas is the object provided by Android on which
a view tries to draw itself. In addition to ShapeDrawable,
there are other subclasses of Drawable like PictureDrawable,
RotateDrawable, ScaleDrawable, ClipDrawable,GradientDrawable, etc
Some of these we will see when we consider the XML approach to
graphics
*/
public GraphicsTestView (Context context)
{
super(context);
setFocusable(true);
this.mDrawable.getPaint().setColor(0xFFFF0000);
//argb where a is alpha (transparency)
}
#Override
protected void onDraw(Canvas canvas)
/* the onDraw method is where a view draws itself
this is our first time overriding it.
*/
{
int x = 10;
int y = 10;
int width = 300;
int height = 50;
this.mDrawable.setBounds(x, y, x + width, y + height);
this.mDrawable.draw(canvas);
ArcShape arc = new ArcShape(45,90); //start angle, sweep angle
ShapeDrawable test = new ShapeDrawable(arc);
Paint p = test.getPaint();
p.setColor(0xFF00FFFF);
p.setStyle(Paint.Style.STROKE);
test.setBounds(10, 70, 310, 370);
//Top-Left, Bottom Right of rectangle to draw into
test.draw(canvas);
}
}
}
Are you saying you want to get the layout view in your XML file? You can draw the views in it, get your code to call it and view it, and then set the images to respond when clicked.
In your onCreate method, after super.onCreate(savedInstanceState); add this setContentView(R.id.layoutname)
For example
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
As for the onClickListener, you can set the views to implement it like this since you already implement it in the Activity.
// set this after "setContentView(R.layout.main);"
b1 = (Button)findViewById(R.id.main);
b1.setOnClickListener(this);
I do not get a grip on solving the issue.
I have manually placed two images on the screen. I would like to have an onClick event handling for each of it.
By the following approach, the OnClick Handler seems to be valid only for the background.
Acually, all created sub-views/Imagers and their onClickListeners are reacting on the parent where they have drawn onto.
The AndroidManifext.xml is only the RelativeLayout with no child tags.
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Imager theUpperImage = new Imager( R.drawable.img1, -25, -100, new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText( v.getContext(), "Upper", Toast.LENGTH_SHORT).show();
}
});
addContentView( theUpperImage, new View.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final Imager theLowerImage = new Imager( R.drawable.img2, -25, +50, new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText( v.getContext(), "Lower", Toast.LENGTH_SHORT).show();
}
});
addContentView( theLowerImage, new View.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
class Imager extends View {
final int resImage;
final int centerOffsetX, centerOffsetY;
public Imager(int inResImage, int inCenterOffsetX, int inCenterOffsetY, View.OnClickListener inOnClickListener) {
super( Main.this );
resImage = inResImage;
centerOffsetX = inCenterOffsetX;
centerOffsetY = inCenterOffsetY;
super.setOnClickListener( inOnClickListener );
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resImage);
final int x = (canvas.getWidth()/2)+centerOffsetX;
final int y = (canvas.getWidth()/2)+centerOffsetY;
canvas.drawBitmap(bitmap, x, y, null);
}
#Override
public void draw(Canvas canvas) {
super.draw( canvas );
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resImage);
final int x = (canvas.getWidth()/2)+centerOffsetX;
final int y = (canvas.getWidth()/2)+centerOffsetY;
canvas.drawBitmap(bitmap, x, y, null);
}
}
}
How to created freely placed images and provide them with an OnClick-functionality?
I would not stick to the above solution. Important for me is to freely place the images on the screen and have them clickable. The predefined Layouts semm not to give me a certain Layout which have a FreeLayout-functionality. (Or is the intention of Layout misleading me?)
I also keep in mind that I probably want to dynamicly remove an Image later, but I am not there yet.
Thank you a lot for help in advance,
Dirk