Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating wide picture as background in Android

I have problem translating very large picture in Android. What I would like is my picture's height to be set screen's height and to show the rest of width not changing image's ratio. Since my image is very wide, not whole would be shown for first, but it would with translation, I would slowly move it over x axis.

I think there must be simple way to do this, but I'm struggling already 6 hours watching all question here and other, trying to find where I'm mistaking because I can't show my picture like that it's shown just as much as it can be shown - height set to full screen's height and width just as much as it fit and then to show other part of picture's width as it's translating.

My code:

import android.os.Bundle;
...

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 

ImageView imageView = (ImageView) findViewById (R.id.background);
ObjectAnimator mover = ObjectAnimator.ofFloat(imageView, "translationX", 0, 1000f);
mover.setDuration(10000);     
mover.start();
}
}

and simple layout:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/background"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/background"
        android:scaleType="centerCrop">

The thing with scaleType with centerCrop and center is that they same do both - they maintain image's ratio and height is screen's height, but the image is cut and when I make animation, immediately starts to go white screen as if there is no more width of picture. centerInside is not helping either.

Can you please help me? I already lost so many hours on this. I would be really thankful.

like image 812
Tomprif Avatar asked Jan 01 '26 04:01

Tomprif


1 Answers

One option here is to place your ImageView inside of a ScrollView. After you do this you can create a timer such as a TimerTask that slowly scrolls the image across the screen every few milliseconds.

You can find out more about Timers here: Android timer? How-to?

And once the timer is setup simply call scrollView.scrollBy(10, 10) or scrollView.scrollTo(10, 10) from within the TimerTask to move the ScrollView which in turn translates your attached ImageView.

like image 66
Gatekeeper Avatar answered Jan 02 '26 20:01

Gatekeeper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!