App not drawing - android

I have my class:
public class CustomDrawableView extends View{
private ShapeDrawable shapeDrawable;
public CustomDrawableView(Context context) {
super(context);
shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.getPaint().setColor(Color.GREEN);
shapeDrawable.setBounds(20,20,20,20);
}
protected void onDraw(Canvas canvas){
shapeDrawable.draw(canvas);
}
}
Then in the main activity I have this code in the oncreate function:
private CustomDrawableView customDrawableView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customDrawableView = new CustomDrawableView(this);
setContentView(customDrawableView);
}
For some reason, it doesn't draw though. I have my CustomDrawableView class which I have created an instance of in the oncreate method in mainactiviy. I have set the content view to the instance of my class too.
Also, is this a good way to create graphics for a game I want to make. Thanks

Check this call:
shapeDrawable.setBounds(20,20,20,20);
the parameters are left, top, right, bottom, so it effectively sets the size of the drawable to 0.

Related

How do I change a custom View color at runtime?

My class is defined as the following:
public class Island extends View {
private ShapeDrawable mDrawable;
public Island(Context context) {
super(context);
int width = 50;
int height = 50;
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(0, 0, width, height);
}
public void change() {
mDrawable.getPaint().setColor(Color.BLACK);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
Why won't the shape change colors when I call change() on the object? Thanks.
you need to call invalidate so the view knows that it needs to redraw
Suppose your object is ,
Island island = new Island(context);
To update your view you can either call island.onDraw()
Or
island.invalidate();
If you are changing this from another thread then you can use postInvalidate() method .

TextView not appearing on canvas

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!

Drawing several layers onto ImageView

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

add view to scrollview in android

I have a relative layout inside a scrollview, then i created a new view with this code:
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(5, 5, 140, 140, paint);
}
}
Then i try to use layout.addView(DrawView) to add the new view to the relative layout, so it can be scrollable with the rest of the content, but is doesn't work, nothing shows up..
Am i missing something ?
Edit:
DrawView formas;
GifMovieView gif;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout layout = new RelativeLayout(this);
final ScrollView scrollView = new ScrollView(this);
//gif = new GifMovieView(this, t_img);
formas = new DrawView(this);
layout.addView(formas);
scrollView.addView(layout);
this.setContentView(scrollView);
inicializar();
load(layout);
}
You will probably want to override onMeasure() in your DrawView. Otherwise your view will have size of 0x0 pixels.
You can start with something as simple as
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(200, 200);
}
just to prove it works, but you will need to do some more meaningful, based on what are contents of your view.
Start from this article to see help on overriding onDraw() and onMeasure()

How to create a view on an activity

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);

Categories

Resources