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
Related
I have displayed an image on canvas.Below is the code.Is there any way to dynamically stretch or shrink image when the image is dragged by user in various directions? How to approach it?
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View
{
public MyView(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(_scratch, 0, 0, null);
}
}
}
In my case, this also worked for stretching
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Or you can try this way
http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/
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.
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!
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