How to use an ImageSwitcher without a Gallery - android

In looking at the ApiDemos example in the Android (1.5) SDK, there is a fine example of using an ImageSwitcher, with a Gallery object to provide the "change image" actions.
The application I'm looking to write, starting into Android development, has three images that I want to be able to pane/scroll through, so ImageSwitcher looks like a fine solution. However, I don't [necesarily] want to have thumbnails in a gallery. I want either a swipe action, and/or a button, to cause a scroll to the previous/next image in the set.
The example ImageSwitcher in ApiDemos uses a Gallery, and without that Gallery, doesn't do anything.
If someone has a suggestion of a way to bind some sort of button controller, or a U/I swipe object, I would appreciate the pointer.
Sorry to ask such a newbie level question.
Thanks.

You can use it like this:
public class GalleryActivity extends Activity implements ViewFactory{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
iSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
iSwitcher.setFactory(this);
iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
iSwitcher.setImageDrawable(fetchImage(mImageURLS[0]));
iSwitcher.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// CLICK HANDLER
// Change image like:
// iSwitcher.setImageDrawable(fetchImage(mImageURLS[1]));
}
});
}
#Override
public View makeView() {
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new
ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}
}
fetchImage(mImageURLS[0]) returns Drawable object

Related

Crossfading back and forth two ImageViews using 2 functions

I'm trying to crossfade two ImageViews by invoking a method when the first image is clicked, then we fade into the second image(alpha set to 0 initially), then I would like to fade back into the first image after clicking on the second image.
It works when crossfading from one image to the other using only one method, but if when I add the other method to crossfade back to the previous image, nothing happens when I click on the image.
public class MainActivity extends AppCompatActivity {
public void narutoFade(View view){
ImageView naruto =(ImageView) findViewById(R.id.naruto);
ImageView narutosage =(ImageView) findViewById(R.id.narutosage);
naruto.animate().alpha(0f).setDuration(2000);
narutosage.animate().alpha(1f).setDuration(2000);
}
public void narutoSageFade(View view) {
ImageView naruto2 = (ImageView) findViewById(R.id.naruto);
ImageView narutosage2 = (ImageView) findViewById(R.id.narutosage);
narutosage2.animate().alpha(0f).setDuration(2000);
naruto2.animate().alpha(1f).setDuration(2000);
}
}
Maybe you should think about declaring both as Fields, declare them only once in onCreate()/onResume(), and write one crossfade method that serves both :
public class MainActivity extends AppCompatActivity {
private ImageView naruto, narutosage;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
naruto =(ImageView) findViewById(R.id.naruto);
narutosage =(ImageView) findViewById(R.id.narutosage);
}
public void crossfade(View fadeIn, View fadeOut) {
fadeIn.animate().alpha(1).setDuration(2000);
fadeOut.animate().alpha(0).setDuration(2000);
}
}
and in your Button onClicks you just call :
crossfade(naruto, narutosage);
or
crossfade(narutosage, naruto);

GIF Listener in android

i'm using this How to animate .gif images in an android? solution (first answer of Shobhit Puri) to implements in my app some Gif. now i want to add some Listener on them (click listner).
every single procedure i tried didn't worked.
the idea is:
the gif is made by 3 image, when you click the first image the gif will change in another one, and so on.
Thanks for your attention.
EDIT:
actualy i'm trying this way
findViewById(R.id.ivAnimation).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
}
});
on onCreate() of the mainActivity. the Log is well generated but i can't go over this
I'm not sure about gifs, but if you have separate files, you could do something like this.
Warning: Untested :)
final int[] imgs = new int[] {R.drawable.img1, R.drawable.img2, R.drawable.img3};
int position = 0;
final ImageView gifView = (ImageView) findViewById(R.id.ivAnimation);
gifView.setImageResource(imgs[position]);
gifView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
position = (position + 1) % imgs.length; // wrap around to "restart" gif
gifView.setImageResource(imgs[position]);
}
});

How to make an ImageView popup on click

I want to be able to click on an ImageView in a list and have it popup on click, like in the Tumblr and Path app.
Does anyone know how this can be done?
PS: I've tired using a dialog already.
I know its old question. but ill add this for future reference.
You can use this library to show image as a Popup. https://github.com/chathuralakmal/AndroidImagePopup
final ImagePopup imagePopup = new ImagePopup(this);
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/** Initiate Popup view **/
imagePopup.initiatePopup(imageView.getDrawable());
}
});
There's a component called PopupWindow, you can find some example of usage here:
http://android-er.blogspot.com.br/2012/03/example-of-using-popupwindow.html
It is slightly similar to a Dialog, but you have more control over it.

Why does my animation freeze when returning to activity containing it?

I'm still sort of new with Android, so forgive me if it's an obvious mistake. In this activity I'm using ViewPager to horizontally scroll through three layouts containing an ImageButton that has an animated background depending on its current state. When the button is pressed, it starts a new activity. However, when I hit the back button to go back to the activity containing the animation from the new activity, sometimes the animation freezes or plays back faster than it should. I wrote a method for starting up the animation that I use in onWindowFocusChanged(), and onRestart(). I'm working in Android 2.1 (API 7).
This is my code:
public class CopyOfWorld extends Activity{
MediaPlayer muzak;
Boolean mSwitch = false;
ImageButton holmes;
AnimationDrawable holmesAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.world);
Preferencer pp = (Preferencer)getApplicationContext();
ViewPagerAdapter adapter = new ViewPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
if(pp.getMuzak()){
mSwitch = true;
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
}
public void clicker(View v){
Intent myIntent = new Intent(CopyOfWorld.this , Subworld.class);
startActivityForResult(myIntent, 0);
}
#Override
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
beginRender();
}
#Override
protected void onPause(){
super.onPause();
if(mSwitch){
muzak.release();
}
}
#Override
protected void onRestart(){
super.onRestart();
Preferencer pp = (Preferencer)getApplicationContext();
if(pp.getMuzak()){
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
beginRender();
}
public void beginRender(){
ImageButton holmes = (ImageButton) findViewById(R.id.subworlder);
StateListDrawable background = (StateListDrawable) holmes.getBackground();
Drawable current = background.getCurrent();
if(current instanceof AnimationDrawable){
holmesAnimation = (AnimationDrawable) current;
holmesAnimation.start();
}
}
}
I've tried calling the method beginRender() under ()onResume, but then the app simply crashes.
Could anyone point me in the right direction?
EDIT:
I've been tweaking the code here and there, unfortunately to no avail. But I did notice a pattern in the behavior of the animation. When I press down on the ImageButton or hold it so that it goes from its default animation to its pressed or focused animation then move my finger away from the button so that it doesn't start up the new activity, it sometimes behaves very much as I described at the beginning of this post (i.e. it's supposed to return to its default animation, but instead plays back at twice the rate, chokes up, or doesn't play at all.)
Currently the xml that contains these ImageButtons defines their backgrounds as the animations and have no source (src). But when I change the background to transparent and the src to the animations, the app crashes.
Any clues?

I need to add onClickListener to a bunch of ImageButtons

I have created a bunch of ImageButtons programmatically while in a for loop. They have worked fine as the data displayed in a HorizontalScrollView. Now I need each one to go dim or bright when clicked. First click will setAlpha(45); second click will setAlpha(255);.
I don't think I fully understand how the Views and onClickListener works yet. It seems the onClick function examples I find take a View. How would that function know which button is clicked? Perhaps there is an easier way to do what I want?
Here are the ImageButtons.
TableRow tr0 = new TableRow(this);
tr0.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
for(int but=0; but<ClueList.size(); but++){
ImageButton clueBut = new ImageButton(this);
clueBut.setBackgroundResource(0);
clueBut.setImageBitmap(ClueList.get(but).btmp);
//clueBut.setOnClickListener(this);
tr0.addView(clueBut);
}
Is there something I need to do to make the buttons identifiable? And how would that pass in through into the onClick function to be used?
-: Added Information :-
I am starting to wonder if the problem isn't with the buttons, but with the way I built the screen. More information added.
The Game activity is the main game, which uses the PuzzleView for the upper part of the screen holding the game grid. The lower part is where the ImageButtons are and I built them in place in the Game class.
public class Game extends Activity{
//various variables and stuff
private PuzzleView puzzleView; // The PuzzleView is from another .java file
// public class PuzzleView extends View
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout mainPanel = new LinearLayout(this);
mainPanel.setOrientation(LinearLayout.VERTICLE);
puzzleView = new PuzzleView(this);
mainPanel.addView(puzzleView);
HorizontalScrollView bottom = new HorizontalScrollView(this);
mainPanel.addView(bottom);
TableLayout clues = new TableLayout(this);
bottom.addView(clues);
TableRow tr0 = new TableRow(this);
for(int but=0; but<ClueList.size(); but++){
ImageButton clueBut = new ImageButton(this);
clueBut.setImageBitmap(ClueList.get(but).btmp);
tr0.addView(clueBut);
}
When I try to add the ClickListener(this) I get errors about this not being able to be a Game. I have similar problems in the onClick(View v) function referencing the View. Are these problems because I am building the buttons in the Game Activity instead of a View class?
Thanks
When you set up an OnClickListener and implement the onClick(View v) callback, it's the Dalvik VM the one that will call that method each time the View is clicked, and it will pass the View instance as a parameter. Thus, the code you write inside that method will be applied only to the View that received the click and not to any other View. Add something like this to your loop:
clueBut.setOnClickListener(new OnClickListener() {
public void onClick (View v) {
if (v.getAlpha() == 1f)
v.setAlpha(0.2f);
else
v.setAlpha(1f);
}
});
In the onClick event:
public void onClick(View currentView)
{
Button currentButton = (Button)CurrentView;
//Do whatever you need with that button here.
}
To identify each view uniquely use the property
View. setId(int)
In your case the code would look something like this
for(int but=0; but<ClueList.size(); but++){
ImageButton clueBut = new ImageButton(this);
clueBut.setBackgroundResource(0);
clueBut.setImageBitmap(ClueList.get(but).btmp);
clueBut.setId(but);
//clueBut.setOnClickListener(this);
tr0.addView(clueBut);
}
Inside the onclick listener match the id of the view using findViewByID()

Categories

Resources