Я хочу реализовать ZoomOutPageTransformer
в моем viewpager
. Я могу автоматически поменять view page
по таймеру, но не вижу смены карт, только текстовые изменения. Кажется, карта стационарная. Только когда я касаюсь, чтобы изменить карты, происходит уменьшение масштаба. Но я хочу, чтобы мои карты перемещались в режиме уменьшения автоматически, не касаясь.
Вот фрагмент кода, который я использую. Я что-то здесь не так делаю?
Моя основная деятельность содержит это:
viewPager = (ViewPager) findViewById(R.id.viewPager);
/*After setting the adapter use the timer */
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
String[] imagesName = new String[6];
imagesName[0] = ("Temperature & Day" + "\n" + "\r" + "0" + "'" + "c" + "\r" + "\r" + "Cloudy");
imagesName[1] = ("Temperature" + "\n" + "\r" + BitwiseOperation.CabinTemp + "\r" + "'" + "c");
imagesName[2] = ("Trip" + "\n" + "\r" + (trunc_lastvalueodometer - trunc_odometer) + "\r" + "\r" + "Kms");
imagesName[3] = ("Range" + "\n" + "\r" + drive_voltage_percentage.expected_range + "\r" + "\r" + "Kms");
imagesName[4] = ("OdoMeter" + "\n" + "\r" + trunc_odometer + "\r" + "\r" + "Kms");
//Initializing the Adapter for images
PagerAdapter adapter = new CustomAdapter(MainActivity.this, imageId, imagesName);
viewPager.setAdapter(adapter);
if (currentPage == NUM_PAGES - 1) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
viewPager.setAnimationCacheEnabled(true);
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
}
};
timer = new Timer(); // This will create a new Thread
timer.schedule(new TimerTask() { // task to be scheduled
@Override
public void run() {
handler.post(Update);
}
}, DELAY_MS, PERIOD_MS);
Мой CustomAdapter
содержит это:
public class CustomAdapter extends PagerAdapter {
private Activity activity;
private Integer[] imagesArray;
private String[] namesArray;
private boolean animationEnabled=true;
public CustomAdapter(Activity activity,Integer[] imagesArray,String[] namesArray) {
this.activity = activity;
this.imagesArray = imagesArray;
this.namesArray = namesArray;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = ((Activity)activity).getLayoutInflater();
View viewItem = inflater.inflate(R.layout.image_item, container, false);
TextView textView1 = (TextView) viewItem.findViewById(R.id.textview);
textView1.setText(namesArray[position]);
((ViewPager)container).addView(viewItem);
return viewItem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imagesArray.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((View)object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
И ZoomoutpageTransformer
здесь:
public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
view.setTranslationX(horzMargin - vertMargin / 2);
} else {
view.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
// Fade the page relative to its size.
view.setAlpha(MIN_ALPHA +
(scaleFactor - MIN_SCALE) /
(1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}