35363

How to insert an image with canvas?

Question:

I´m trying to insert a simple image with canvas in Android with the next code, but when i try to execute in the smartphone, only appears a white screen without image.

import android.support.v7.app.ActionBarActivity; import android.content.Context; import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); } } class Lienzo extends View { private Drawable theimage; public Lienzo(Context context) { super(context); Resources res = context.getResources(); theimage = res.getDrawable(R.drawable.ic_launcher); theimage.setBounds(30, 30, 200, 200); } protected void onDraw(Canvas canvas) { theimage.draw(canvas); } }

I´m sure this is a foolishness but, what´s wrong in the code? The idea is simple, insert an image in the canvas.

Answer1:

I found the answer: I add a view (Lienzo) to the layout. Answer found in <a href="http://www.javaya.com.ar/androidya/detalleconcepto.php?codigo=175&inicio=40" rel="nofollow">this spanish web</a>.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1); Lienzo image = new Lienzo(this); layout1.addView(image); }

Thanks Xavier Falempin, you inspired me.

Recommend

  • Android/MongoDB: “NoClassDefFoundError: com.mongodb.DBPortPool”
  • “class template has already been declared as a non-class template”
  • Tiny Images, No Rotation, but still get OutOfMemoryError: bitmap size exceeds VM budget
  • Android ripple for button not working
  • Android how to set a transparent image resource of a view? or Remove the image?
  • Change Design Support Navigation View Header Title Programmatically
  • How to Set wallpaper of image with its actual size?
  • Recreate Apple Watch fitness tracker ‘progress’ bar - gradient on CAShapeLayer stroke
  • draw a B+ tree in latex
  • Google Player Game Service Error: Can't build the sample game
  • Android - HorizontalScrollView will not scroll all the way
  • Winforms: Add a close “x” button in a UserControl
  • write text on image and show it to a imageview
  • What is the equivalent of Android permissions in iOS development? [duplicate]
  • Unable to resolve Static method?
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • How to resolve permission denied maybe missing internet permission?
  • How to draw a line dynamically in android [duplicate]
  • Bash if statement with multiple conditions
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • JBoss External Properties Files in Classpath
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Using a canvas object in a thread to do simple animations - Java
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Code in Job's Script Block after Start-Process Does not Execute
  • Chart.js Multiple dataset
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Javascript Callbacks with Object constructor
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Apache 2.4 - remove | delete | uninstall
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Cannot Parse HTML Data Using Android / JSOUP
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • Android Heatmap on canvas or ImageView