I create one simple image banner apps in which I use the viewflipper for swipe image one by one. My apps run very well but the first image is repeated two time. Here I use the image array and apply in view flipper.
The Array is:
int imageDrawable[] = new int[]{R.drawable.advanturestorybanner_ipad_landscap,
R.drawable.femouspeopleofthebiblebanner_ipad_landscap,
R.drawable.littlebanner_ipad_landscap,
R.drawable.thechildrenbiblebanner_ipad_landscap,
R.drawable.thecomicbookbiblebanner_ipad_landscap,
R.drawable.thehandybiblebanner_ipad_landscap };
bannerImageView.setBackgroundResource(imageDrawable[j]);
Related
I'm using this code to dynamically pop images on the slider using Image Flipper.
flipper= (ViewFlipper) findViewById(R.id.flipper1);
for(int i=0;i<6;i++)
{
// This will create dynamic image view and add them to ViewFlipper
Button image = new Button(getApplicationContext());
image.setBackgroundResource(R.drawable.stock_bg);
image.setText("Sps"+i);
// image.getLayoutParams().height=40;
// image.getLayoutParams().width=40;
flipper.addView(image);
}
I have set sliding animation to it. The issue is that it fits one image on the whole screen width and then animates to other. While I want the image size to remain same and it should not fit it to the entire screen. Also note that if I change the flipper width from wrap content to custom width, it will not slide the entire screen. Putting it simple, how can I force the flipper to show more then 1 image on the screen at a time.
I have a grid layout where the number of rows and columns in it varies depending on the value of an int that is parsed to the activity. I'm now trying to randomly put one of the 12 images I have into each of the grid spaces to get a random mix of images in a square on the screen. So how do I put drawables into grid spaces without using xml and would a table layout be better? I will also need to be able to detect what image is in the grid space when it is clicked on.
I had the same problem while implementing a memory game some time ago.
To get a drawable object you first need to get your app resources.
Resources res = getResources();
Then, what I do for generate the random position is to put all my images in an ArrayList that has the number of elements equal to the number of grids your have:
ArrayList<Drawable> imagesArray = new ArrayList<Drawable>();
imagesArray.add(res.getDrawable(R.drawable.yourdrawablename));
imagesArray.add(res.getDrawable(R.drawable.yourdrawable2name));
//and so on for the following drawables you have.
After that, you could generate a random number using your ArrayList's size, that is the same amount of images you have, and after that, you put the images on your grid and remove it from the array.
for(int i = 0 ; i < imagesArray.size() ; i++) {
int randomPosition = Random.nextInt(imagesArray.size());
//here's the code to put your drawable in your grid view, in my case, it was just a button
//background, so I didn't know exactly how to put it inside a position of the grid, but it
//should not be that difficult to get it from documentation
//In my case, I do:
button.setBackground(imagesArray.get(randomPosition));
//And then delete that position on the array...
imagesArray.remove(randomPosition);
//And repeat until array's length is null;
}
For the last question, for discovering witch drawable is in which grid, you could get the constantState of it like this:
gridViewElement.getBackground();
This will return the background as a drawable that is associated with that gridView.
Also, be aware that if you want to compare to drawables inside two differente grids, you should use this:
if(gridViewElement1.getBackground().getConstantState()
.equals(gridViewElement2.getBackground().getConstantState())) {
//code goes here.
}
Take a look at this design I did for an interface of a game I want to develop on Android:
http://www.moboing.com/smallshow.jpg
When a user touches a balloon, I want that star/glare animation to play with the stars dropping down. All I'm curious about is the best general direction/approach to making that animation possible while developing the app in eclipse/java.
I was thinking making a few variations of stars as transparent PNG's, and making them animate on touch but I'm a newbie so I'm not entirely sure.
You can create the animation like a sequence of images and then use the AnimationDrawable class to make it animated. Let me show you an example:
ImageView my_image = (ImageView) findViewById(R.id.my_animation);
AnimationDrawable animation = new AnimationDrawable();
// We need to add each image to our final animation setting time interval between them
animation.addFrame(getResources().getDrawable(R.drawable.img_1), 200);
animation.addFrame(getResources().getDrawable(R.drawable.img_2), 200);
animation.addFrame(getResources().getDrawable(R.drawable.img_3), 200);
animation.setOneShot(false);
my_image.setBackgroundDrawable(animation); // Set as background to see it
animation.start(); // Start playing the animation
Where:
my_animation: your ImageView into your desire Layout.
img_1, img_2, img_3: images that compounds your animation (your should
create and save them into res/drawable folder).
I hope it can help you and draw a way to do it by yourself :)
PS: this should be into a method on the activity that you want (for example, into onCreate to show it when user start).
I was attempting to place a image on top of another existing image in android. Here was the game plan. First the user would select a button and then that button would tell the program to populate a sprite/ image to a fixed location on the master Image. Once the image is set the user could then hit a button for another image on top of what is currently their, (the goal is two images with a option to change their size, x and y position. I am only using android 2.1 platform! The button for selection and the text describing the activity are in a linearlayout.
You should use a RelativeLayout and then you can use params to set position, margin, etc..
something like this:
//this is your fist item
r1.setImageResource(R.drawable.myimage);
//this is the second item
r2.setImageResource(R.drawable.myimage2);
RelativeLayout.LayoutParams imageParams=
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageParams.setMargins(1, 1, 1, 1);
imageParams.addRule(RelativeLayout.BELOW,r1.getId());
addView(r2,imageParams);
I'm trying to make my own custom view, currently all it does is draw an image on a specific x and y coordinate and then draws the similar images repeatedly on different locations.
I want to be able to create a button on each instance of the image that is drawn. if one image is clicked it will have cause something different to happen depending on which image is chosen.
How can I implement this?
would I have to create a different view for each image/button combination and then set an onClick event?
Let me try to be a little more clear
I'm trying to make a map using hexagon (different types of terrains for different players)
I've figured out how to get them to draw (see here - they will have a border to show what terrain is owned by whom)
I just made a custom view class and had the hexagons drawn using a Canvas; however, I'm not sure how to be able to make the hexagons into buttons so that I can differentiate between which hexagon was chosen and how it should react to the opponents spot.
I was thinking of making a ViewGroup called Terrain to contain the Nodes(hexagons) that belong to the player and have a group of Node Views that only draw the hexagon where it should be located.
the question is can i make each node or the entire viewGroup into a button (or do an onTouch ) if a certain hexagon is pressed?
If I understood well, you should do :
One template my_pictures.xml which is a simple button.
Then you create a custom adapter which as a function for each kind of image you wanna create. By this, I mean that you will have to change the background value of your button depending on what you need it to be. Then you do a notifyDataSetChanged(); to refresh the container with the new button.
For the Listener, you just have to add it either in your activity or your adapter when you create your buttonImages, I don't know what's better.
Thanks for your help!
I figured out what I needed to do.
I have a NodeView class and in my GameActivity class, I'm using a relative layout and setting the layout parameters of where I wanted things positioned
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(95,95);
params.leftMargin = 10;
params.topMargin = 10;
params = new RelativeLayout.LayoutParams(95,95);
params.leftMargin = 10+95*x;
params.topMargin = 81+(71*y);
rl.addView(new NodeView (this,0,0,1,1), params);
This helped me add things where I needed them and now all I'm trying to figure out how to scroll around the territories on both the x and y axis (I tried ScrollView but that only allows me to scroll on the y-axis, I'm looking into it though)