Android program quits trying to display a jpg image - android

Here is my program.
public class MathsExplorer extends Activity {
/** Called when the activity is first created. */
private GraphicsView view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
}
}
public class GraphicsView extends View {
public GraphicsView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
String background = "/Maths Explorer/bin/res/drawable/MC900436279.jpg";
Bitmap b = BitmapFactory.decodeFile(background);
canvas.drawBitmap(b, 10, 10, null);
}
}
It quits right away with the error "Unfortunatly, Maths Explorer has stopped."
Can anyone help me? I'm not sure, I'm just trying to display an image.

I think that path to file is wrong, you can't do that.
Try this.

Related

Button in Android Studio interacting with View

I am working on an android application with a DrawingView.
I would like to have a button which clears the drawing view, however I cannot figure out how to make a button that calls a method in somewhere that's not the MainActivity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
The above text is the only code in the main activity, so I do not have access to a reference of the drawingView.
public DrawingView(Context context, AttributeSet attributeSet){
super(context, attributeSet);
AsyncTask myTask = new AsyncTask() {
#Override
protected Object doInBackground(Object... objects) {
try {
DrawingView dView = (DrawingView) objects[0];
dView.connection = new Connection((DrawingView) objects[0], (Context) objects[1],new Socket("128.199.236.107", 3333));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
Object[] myObjects = new Object[2];
myObjects[0] = this;
myObjects[1] = context;
myTask.execute(myObjects);
getRes();
setupDrawing();
This is the constructor of the class DrawingView.java
There is a method in this class called "refresh", and I am trying to call it using a button. Is there any way I can do this?
The refresh method looks like this
public void refresh(){
Canvas.drawColor(0, Mode.CLEAR);
}
The MAIN ACTIVITY refresh method (called on the button press), looks like this
public void refresh(View v){
final DrawingView DV = (DrawingView) findViewById(R.id.drawing);
DV.refresh();
}
Logcat? I think
Make your shared method static by adding the "static" keyword:
public static void myFunction()
Then use:
Another Activity.myFunction();
One simpe way to cleanup your canvas is to insert this code on the OnDraw method:
Paint p=new Paint();
p.setARGB(255,100,120,140); // your background color
Rect r =new Rect();
canvas.getClipBounds(r);
canvas.drawRect(r,p);`
but it's better to perform the allocations of r and p outside of onDraw.

onClickListener set on Activity

I have a class SomeView that extends View and that is displayed in a class Controls that extends linear layout.
The linear layout is instantiated onCreate of an activity.
I would like to call a method in the activity every time I click on this view SomeView.
I have tried to set an onClickListener in the activity like this
public class MainActivity extends AppCompatActivity implements
SomeView.OnClickListener {
private Controls menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
menu = new Controls(this);
menu.getSomeView().setOnClickListener(this);
setContentView(menu);
}
#Override
public void onClick(View view) {
System.out.println("Hello");
}
}
The controls class looks like this
public class Controls extends LinearLayout {
private SomeView aview;
public Controls(Context context) {
super(context);
this.setOrientation(LinearLayout.HORIZONTAL);
aview = new SomeView(context);
this.addView(aview, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
public SomeView getSomeView() {
return aview;
}
}
and the SomeView class looks like this (it just draws an oval)
public class SomeView extends View {
public SomeView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF aRect = new RectF();
aRect.left = getPaddingLeft();
aRect.top = getPaddingTop();
aRect.right = getWidth() - getPaddingRight();
aRect.bottom = getHeight() - getPaddingBottom();
Paint aPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
aPaint.setColor(Color.GREEN);
canvas.drawOval(aRect, aPaint);
}
}
But I am missing something because clicks are not calling the onClick method.
What else do I need to set up?
it seems like you did only mistake in your MainActivity class, where you forgot to call the super method. Try doing this, hope it will work, since it works from here in my mobile.
Main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
menu = new Controls(this);
menu.getSomeView().setOnClickListener(this);
setContentView(menu);
}
And in your callback method, instead using System.out.println(), use Log.d() as below:
#Override
public void onClick(View view) {
Log.d(TAG, "Hello");
}
and it is working from here, look at the image below as well.

Unable to draw a line in android using views

This is the java part of the code
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
class surf extends View {
public surf(Context c,AttributeSet as) {
super(c,as);
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.MAGENTA);
canvas.drawLine(20, 20,200, 20,p);
}
}
}
And the resource file for the same without other views
<view
class="tictac.Tictac.MainActivity$surf"
android:id="#+id/graphics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
It closes as soon as it opens with a error, I have been following the tutorial in the android website
Try this code. Here I am adding own view as main view.
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new surf(this));
}
class surf extends View {
public surf(Context c)
{
super(c);
}
public surf(Context c,AttributeSet as) {
super(c,as);
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.MAGENTA);
canvas.drawLine(20, 20,200, 20,p);
}
}
}
Or you can add your view in XML
<com.my.package.MyClass
android:id="#+id/graphics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

Android: can I call a Log.i method from the onDraw method of a view?

I am trying to call the Log.i method to test the refresh rate for the View:
public class GameView extends View {
public GameView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Log.i("OnDraw","ping");
invalidate();
}
Why doesn't this work?
public class GameUI extends Activity implements OnClickListener, OnTouchListener {
/** Called when the activity is first created. */
GameView gameview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameview = new GameView(this);
setContentView(gameview);
}
Try this
public GameView(Context context)
{
super(context);
this.setWillNotDraw(false);
}
This may help you..........

Android : Canvas.drawBitmap() or Imageview.setImageBitmap(bitmap)

Suppose I want to create an activity that displays 12 different PNG images.
Should I create a class extending View (let say "MyView") in which I would use canvas.drawBitmap(...)?
public class MyActivity extends Activity {
private MyView myView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = new MyView(this);
setContentView(myView);
myView.requestFocus();
}
...
}
public class MyView extends View {
#Override
protected void onDraw(Canvas canvas) {
// do that 12 times ...
canvas.drawBitmap(...)
}
}
Or should I use 12 ImageView objects and set the bitmap in each one of it?
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout root = new LinearLayout(this);
// do that 12 time ...
ImageView imageView = new ImageView();
Bitmap bitmap = BitmapFactory.decodeFile(...);
imageView.setImageBitmap(bitmap);
root.addView(imageView);
}
...
}
I think creating an ImageView will have more overhead.
However, it will provide more easily implemented functions

Categories

Resources