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
Related
I have done I game in which I want to call on a method I have in a view from another view. I figured I would somehow have to send the "first view" into the "second view" through the my MainActivity in order for the second view to be able to call on the first view methods. However, I couldn't come up with any way of sending in the first view to the second view through my MainAcitivity, so I decided to change tactics. I now tried to have a function in my MainActivity to handle the interection between the views, but once again I was not able to call on the method from the second View.
Therefore my question is how do you send a view into another view through an Activity, or If that's not possible how do you call on an activity method through a view?
Here is the code (I added some comments to better show the problem I'm having):
public class MainActivity extends AppCompatActivity {
private FishView gameView;
private SmallBall smallBall ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout screen = findViewById(R.id.gameScreen);
gameView = new FishView(this);
smallBall = new SmallBall(this);
screen.addView(gameView); // first view
screen.addView(smallBall); //second view
}
//this is the method I want to reach through the View
public void handleAvoidedBall(){
gameView.avoidedBall();
}
}
public class SmallBall extends View {
private final Bitmap sodaCan;
private final static long smallBallPeriod = 60;
private final Handler handler = new Handler();
public SmallBall(Context context) {
super(context);
Paint smallBall = new Paint();
smallBall.setColor(Color.GRAY);
smallBall.setAntiAlias(false);
resetBall();
sodaCan = BitmapFactory.decodeResource(getResources(),R.drawable.sodacan);
Timer movementTimer = new Timer();
movementTimer.scheduleAtFixedRate(smallBallTask, 0, smallBallPeriod);
}
private final TimerTask smallBallTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
invalidate();
if (isBallLanded()){
//Here I want to call on a handleAvoidedBall() in MainActivity
//OR simply have gameView here if possible
// gameView.avoidedBall();
//OR
//SomeMainAcitvityObject.handleAvoidedBall();
}
}
});
}
};
#Override
protected void onDraw(Canvas canvas) {
..... //Do stuff}
}
So as I hopefully have explained somewhat decent now, I'm wondering how to either send gameView into the SmallBall view OR how to call on handleAvoidedBall() in MainActivity from the SmallBall view?
Thank you for your time and hope you have a wonderful day!
Your best option would be to define a listener that you would set on the SmallBallView.
Define the listener:
public interface BallListener {
void onAvoided(SmallBall ball);
}
And then inside your SmallBall class, you would have this method:
public void setListener(BallListener listener){
this.listener = listener;
}
And then call this in your activity, after you've instantiated the SmallBall class:
smallBall.setListener(new SmallBallListener(){
#Override
public void onAvoided(SmallBall ball){
// Do stuff here
}
})
As #LukeWaggoner mentioned, you should consider using listeners instead of making view static in your activity.
You told us, that you'd like to add more than one SmallBall views, so I figure that you don't want to write a listener's code for each of them.
It is easily doable with MainActivity implementing SmallBallListener.
Listener:
public interface SmallBallListener {
void onAvoidedBall();
}
SmallBall class:
public void setListener(SmallBallListener listener){
this.listener = listener;
}
MainActivity:
public class MainActivity extends AppCompatActivity implements SmallBallListener {
private FishView gameView;
private SmallBall smallBall ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout screen = findViewById(R.id.gameScreen);
gameView = new FishView(this);
screen.addView(gameView); // first view
// Add 10 small ball views
for(int i = 0; i < 10; i++) {
SmallBall ball = new SmallBall(this);
ball.setListener(this); // MainActivity is a listener here, so each ball has the same listener code
screen.addView(ball);
}
}
//this is the method I want to reach through the View
public void handleAvoidedBall() {
gameView.avoidedBall();
}
#Override
public void onAvoidedBall() { // this is the SmallBallListener method
this.handleAvoidedBall();
}
}
So whichever SmallBall view call listener.onAvoidedBall(), it will fire onAvoidedBall() method in MainActivity class.
Turns out all I had to do was to set:
private FishView gameView;
to:
public static FishView gameView;
And then simply use "MainActivity.gameView" in the SmallBall view. This gave me no additional warings either, so that was good also.
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.
I'd like to know how to create buttons, textViews etc. in an Activity, which doesn't use an xml file as ContentView.
What I mean:
Main Class:
protected void onCreate(Bundle savedInstanceState)
{
//...
puzzleView = new PuzzleView(this);
setContentView(puzzleView);
}
The PuzzleView class:
public class PuzzleView extends View
{
private final Game game;
public PuzzleView(Context context)
{
super(context);
this.game = (Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
}
#Override
protected void onDraw(Canvas canvas)
{
// drawing some things here
}
}
You probably want to set your content view to a class that extends ViewGroup, then add views to that class. Layouts are probably what you're after, in particular. Here are some tutorials:
http://blogspot.arcintechnologies.com/android/generate-android-layout-programmatically/
http://mainerrors.blogspot.ca/2011/02/programmatically-creating-layout-part-1.html
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"/>
I have created HideImages() function as shown below. The problem is, that running this code causes NullPointerExcpection. When I comment out the setVisibility lines, it works fine. What am I doing wrong?
public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
ImageView img_w0 = (ImageView)findViewById(R.id.img0);
ImageView img_w1 = (ImageView)findViewById(R.id.img1);
ImageView img_w2 = (ImageView)findViewById(R.id.img2);
HideImages();
}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}
Make all the references of ImageView as Global as
public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
HideImages();
}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}
I think this may case the problem because you had already initialize ImageView above the onCreate() method the why you declare here,
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
The problems is here. You're already declared the ImageView objects as globally. And, again you're declaring internally in onCreate()
So, just remove the declaration inside of onCreate() and run. Like below -
public void onCreate(Bundle savedInstanceState) {
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
}