How to make my image visible by time counter? - java

I want to make simple animation using multi-images on android studio
i have 3 images (img1,img2,img3)
and i want img1 visible firstly then after half second img1 invisible and img2 visible,then after half second img2 invisible and img3 visible,then after half second img3 invisible and img1 visible, so return to the first image like circle
1>2>3>1>2>3>1>2>3 to unlimited time, so how can i do that, please

class ImageAnimation {
Activity activity;
android.os.Handler handler;
ImageView[] images = new ImageView[3];//three images array
int current = 0;//current image iterator
public ImageAnimation(Activity activity) {
handler = new android.os.Handler();
images[0] = (ImageView) activity.findViewById(R.id.you_image1_id);
images[1] = (ImageView) activity.findViewById(R.id.you_image2_id);
images[2] = (ImageView) activity.findViewById(R.id.you_image3_id);
current = 0;
}
public void animateImages() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
//loop for all images and animate it
for (int i = 0; i < images.length; i++) {
if (i!=current)
images[i].animate().alpha(1);//show
else
images[i].animate().alpha(0);//hide
}
if (current<images.length)
current++;
else
current=0;//again first one
//recurring using the same method - infinite loop
animateImages();
}
}, 500); //half a second
}
};

Related

Updating ImageViews within a TilePane with JavaFX (Simple Reference Problem?)

Currently, I am trying to update the ImageViews within each Tile of a TilePane.
Variable at top of class available to all methods: ImageView imgArray[] = new ImageView[20];
Creating the original TilePane:
public TilePane createTilePane() {
TilePane tile = new TilePane();
tile.setPrefColumns(5);
for (int i = 0; i < 20; i++) {
tile.getChildren().add(this.createImageView("https://media.forgecdn.net/avatars/141/115/636539774401917932.jpeg", i));
} //for
return tile;
} //createTilePane()
public ImageView createImageView(String url, int x) {
Image image = new Image(url);
imgArray[x] = new ImageView();
imgArray[x].setImage(image);
imgArray[x].setFitWidth(100.0);
imgArray[x].setFitHeight(100.0);
imgArray[x].setImage(image);
return imgArray[x];
} //createImageView()
The problem is whenever I want to change an image within the tilepane I can't. For example I want to be able to do: this.createImageView("url", 2); to change a Tile within the TilePane.
I can do that with no errors, but the image is not changing.
Any ideas?
createImageView does not update the scene. It simply creates a new ImageView and updates the array containing the data. You need to update the scene, if you want to change the display. The best way of doing this in this scenario is modifying the image property of the ImageView.
Note that I've rewritten your code a bit to reuse the Image instance. createImageView has been renamed to setImage, since this seems like a better name for what it's doing to me:
public TilePane createTilePane() {
// reuse large object
Image image = new Image("https://media.forgecdn.net/avatars/141/115/636539774401917932.jpeg");
TilePane tile = new TilePane();
tile.setPrefColumns(5);
for (int i = 0; i < 20; i++) {
ImageView imageView = createImageView();
imageView.setImage(image);
imgArray[i] = imageView;
tile.getChildren().add(imageView);
} //for
return tile;
} //createTilePane()
private ImageView createImageView() {
ImageView image = new ImageView();
image.setFitWidth(100d);
image.setFitHeight(100d);
return image;
}
public void setImage(int index, String url) {
ImageView imageView = imgArray[index];
Image image = new Image(url);
// change image in ImageView that was added to the scene
imageView.setImage(image);
}

Xamarin Android Vertical ViewPager

I need a vertical ViewPager in Android using Xamarin and the solution doesn't work. I searched some examples in java but there's an object developed by the git community that do all the work. Unfortunately there isn't in Xamarin. So, this is my code, it doesn't give me error, but it displays only a black screen. Nothing more.
I Extended the ViewPager class
public class VerticalViewPager : ViewPager {
public VerticalViewPager (Context context):base(context) {
Init ();
}
public VerticalViewPager(Context context, IAttributeSet attr):base(context, attr) {
Init();
}
public override bool OnTouchEvent (Android.Views.MotionEvent e) {
e.SetLocation (e.GetY (), e.GetX ());
return base.OnTouchEvent (e);
}
private void Init() {
SetPageTransformer (true, new PagerTransformer());
OverScrollMode = Android.Views.OverScrollMode.Never;
}
}
and create my PageTransformer
public class PagerTransformer : Java.Lang.Object, ViewPager.IPageTransformer {
int pageWidth;
int pageHeight;
float yPos;
public PagerTransformer () {}
public void TransformPage (View view, float position) {
pageWidth = view.Width;
pageHeight = view.Height;
if (position < -1) {
view.Alpha = 0;
}
else if (position <= 1) {
view.Alpha = 1;
// Counteract the default slide transition
view.TranslationX = (pageWidth * -position);
//set Y position to swipe in from top
yPos = position + pageHeight;
view.TranslationY = yPos;
}
else {
// This page is way off-screen to the right.
view.Alpha = 0;
}
}
}
In the MainActivity, onCreate method i set
page = FindViewById<VerticalViewPager> (Resource.Id.vertical_pager);
and obviously page is a VerticalViewPager object.
If I use a normale ViewPager, the app works fine. Any ideas about the reason of the black screen?
Any java code is appreciare as well!
Thanks
The reason of black screen is that you set your screen position out of visible bounds. Your visible bounds are from 0 to page height.
Try his : "yPos = position * pageHeight;" instead of "yPos = position + pageHeight;"

Simple Android Live Wallpaper with sequence of images - Updated with code

I would like to develop a simple Live Wallpaper with a sequence of images. The only animation required is for each image to fade in and fade out.
All the tutorials I have found online for LWP demonstrate how to use the Draw canvas for fancy animations and drawing. This is not necessary for my app, I only want to iterate through a collection of images.
Being a novice programmer, I need some help learning how to loop through an array of images and how to display them as a wallpaper.
Can anyone share some code or point me towards a good tutorial for this?
UPDATE
The LWP loads on my device but the wallpaper does not change. It is stuck on image3, ironman
Here is the code I have so far. I assume I am doing something wrong in draw()
public class Wallpaper extends WallpaperService {
public void onCreate() {
super.onCreate();
}
public void onDestroy() {
super.onDestroy();
}
public Engine onCreateEngine() {
return new CercleEngine();
}
class CercleEngine extends Engine {
private final Handler handler = new Handler();
private final Runnable drawRunner = new Runnable() {
#Override
public void run() {
draw();
}
};
private boolean visible = true;
public Bitmap image1, image2, image3;
CercleEngine() {
image1 = BitmapFactory.decodeResource(getResources(),
R.drawable.batman);
image2 = BitmapFactory.decodeResource(getResources(),
R.drawable.captainamerica);
image3 = BitmapFactory.decodeResource(getResources(),
R.drawable.ironman);
}
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
}
#Override
public void onVisibilityChanged(boolean visible) {
this.visible = visible;
if (visible) {
handler.post(drawRunner);
} else {
handler.removeCallbacks(drawRunner);
}
}
#Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
this.visible = false;
handler.removeCallbacks(drawRunner);
}
public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
float yStep, int xPixels, int yPixels) {
draw();
}
void draw() {
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
c.drawBitmap(image1, 0, 0, null);
c.drawBitmap(image2, 0, 0, null);
c.drawBitmap(image3, 0, 0, null);
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
handler.removeCallbacks(drawRunner);
if (visible)
{
handler.postDelayed(drawRunner, 1000); // delay 1 sec
}
}
}
There is no easy way around the looping through an array of images. It would have to be done manually.
One approach that you could adopt is to keep your images in the /res/drawable
and then use an int[] array to store the resid's of the images and then loop through it.
A well explained tutorial on Live Wallpapers may be found here:
http://www.vogella.com/articles/AndroidLiveWallpaper/article.html
Good Luck

AnimatedGIFField from blackberry knowledge base, don't know how to manipulate the gif

I am using this code, if anybody is familiar with it, its from the blackberry knowledge base. Anyway, I was wondering how to manipulate GIF's using this class. I can get the gif on the screen, but it keeps repeating and will not disappear. Any help is greatly appreciated!
public class AnimatedGIFField extends BitmapField
{
private GIFEncodedImage _image; //The image to draw.
private int _currentFrame; //The current frame in
the animation sequence.
private int _width; //The width of the image
(background frame).
private int _height; //The height of the image
(background frame).
private AnimatorThread _animatorThread;
public AnimatedGIFField(GIFEncodedImage image)
{
this(image, 0);
}
public AnimatedGIFField(GIFEncodedImage image, long style)
{
//Call super to setup the field with the specified style.
//The image is passed in as well for the field to
//configure its required size.
super(image.getBitmap(), style);
//Store the image and it's dimensions.
_image = image;
_width = image.getWidth();
_height = image.getHeight();
//Start the animation thread.
_animatorThread = new AnimatorThread(this);
_animatorThread.start();
}
protected void paint(Graphics graphics)
{
//Call super.paint. This will draw the first background
//frame and handle any required focus drawing.
super.paint(graphics);
//Don't redraw the background if this is the first frame.
if (_currentFrame != 0)
{
//Draw the animation frame.
graphics.drawImage(_image.getFrameLeft(_currentFrame), _image.getFrameTop(_currentFrame),
_image.getFrameWidth(_currentFrame), _image.getFrameHeight(_currentFrame), _image, _currentFrame, 0, 0);
}
}
//Stop the animation thread when the screen the field is on is
//popped off of the display stack.
protected void onUndisplay()
{
_animatorThread.stop();
super.onUndisplay();
}
//A thread to handle the animation.
private class AnimatorThread extends Thread
{
private AnimatedGIFField _theField;
private boolean _keepGoing = true;
private int _totalFrames; //The total number of
frames in the image.
private int _loopCount; //The number of times the
animation has looped (completed).
private int _totalLoops; //The number of times the animation should loop (set in the image).
public AnimatorThread(AnimatedGIFField theField)
{
_theField = theField;
_totalFrames = _image.getFrameCount();
_totalLoops = _image.getIterations();
}
public synchronized void stop()
{
_keepGoing = false;
}
public void run()
{
while(_keepGoing)
{
//Invalidate the field so that it is redrawn.
UiApplication.getUiApplication().invokeAndWait(new Runnable()
{
public void run()
{
_theField.invalidate();
}
});
try
{
//Sleep for the current frame delay before
//the next frame is drawn.
sleep(_image.getFrameDelay(_currentFrame) * 10);
}
catch (InterruptedException iex)
{} //Couldn't sleep.
//Increment the frame.
++_currentFrame;
if (_currentFrame == _totalFrames)
{
//Reset back to frame 0 if we have reached the end.
_currentFrame = 0;
++_loopCount;
//Check if the animation should continue.
if (_loopCount == _totalLoops)
{
_keepGoing = false;
}
}
}
}
}
}
Don't call super.paint(graphics), rather draw everything you need to draw by yourself. re-write your paint(Graphics graphics) method like below:
private boolean isPaint = true;
protected void paint(Graphics graphics) {
if(!isPaint) return;
// super.paint(graphics);
if (_currentFrame == _image.getFrameCount()-1) {
graphics.setGlobalAlpha(0);
isPaint = false;
}
graphics.drawImage(
_image.getFrameLeft(_currentFrame),
_image.getFrameTop(_currentFrame),
_image.getFrameWidth(_currentFrame),
_image.getFrameHeight(_currentFrame), _image,
_currentFrame, 0, 0
);
}

how to get images into blackberry and set an onClick listener?

I'm rather new to programming Blackberries and was wondering if anybody knows of a tutorial or a snippet about how to load an image into the screen and set an onClick listener to it?
edit, got this far:
ButtonField btf1 = new ButtonField("Fine!");
ButtonField btf2 = new ButtonField("Great!");
RichTextField rtf = new RichTextField("HELLO, HOW ARE YOU?");
Bitmap LOGO = Bitmap.getBitmapResource("1.png");
BitmapField LogoBmpField = new BitmapField(LOGO);
HelloWorldScreen()
{
setTitle("My First App");
add(rtf);
add(btf1);
add(btf2);
add(LogoBmpField);
}
Thanks!
edit: by the way, how should interfaces be made for blackberry? simply by
ButtonField btf1 = new ButtonField("Fine!");
add(btf1);
Or is there some more visible way, such as in XML for android?
One more thing, how to I change or set properties of some object. Say I want to change the title of my button- btf1.(expecting list of available properties to appear ) doesn't give anything.
Place your image in your res folder and try this;
Bitmap bmpLogo = Bitmap.getBitmapResource("yourImage.jpg");
BitmapField logo = new BitmapField(bmpLogo){
protected boolean trackwheelClick(int status, int time)
{
// Your onclick code here
return true;
}
};
add(logo);
1) You need to make your BitmapField focusable.
I just tried this:
BitmapField LogoBmpField = new BitmapField(LOGO, BitmapField.FOCUSABLE) {
protected boolean trackwheelClick(int status, int time) {
System.out.println(" -- You clicked me! ");
return true;
}
};
and it seems to work.
2) And to change the text on your button use
setLabel()
btf1.setLabel("new button text");
public class MyScreen extends MainScreen {
public LanguageSelector() {
Bitmap logoBitmap = Bitmap.getBitmapResource("normalarabflag.png");
BitmapField LogoBmpField = new BitmapField(logoBitmap, BitmapField.FOCUSABLE | Field.FIELD_HCENTER) {
protected boolean navigationClick(int status, int time) {
System.out.println(" -- You clicked me! ");
UiApplication.getUiApplication().pushScreen(new SecoundScreen());
Dialog.alert("Load Complete");
return true;
}
};
LabelField labelfield = new LabelField("Arabic ",Field.FIELD_HCENTER|LabelField.FOCUSABLE);
VerticalFieldManager vrt=new VerticalFieldManager(USE_ALL_WIDTH) {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(),Display.getHeight());
setExtent(Display.getWidth(),Display.getHeight());
}
};
Font f=labelfield.getFont();
int hight1=f.getAdvance(labelfield.getText());
int k=labelfield.getPreferredHeight();
int number=hight1/Display.getWidth()+1;
int hight2=logoBitmap.getHeight();
int padding=(Display.getHeight()-((number*k)+hight2))/2;
if(padding>0) {
LogoBmpField.setPadding(padding,0,0,0);
}
vrt.add(LogoBmpField);
vrt.add(labelfield);
add(vrt);
}
}

Categories