java - 将滑动功能和 TextView 添加到图像幻灯片 Android 应用程序 - 漫画

标签 java android android-layout swipe-gesture

我需要向图像幻灯片 Android 应用程序实现添加滑动功能和 TextView - 这实际上是一部关于 St. Don Bosco 生活的漫画,其中包含与故事相关的图像。图库小部件完成了这项工作,但遗憾的是现在已弃用:(

我设法执行以下操作,并且只能通过单击底部的缩略图才能在图像之间导航:
Screenshots - Genymotion

当前Java:

package org.dbysmumbai.donbosco;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity implements
        AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.dbyouth_activity);

        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));

        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemSelectedListener(this);
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        mSwitcher.setImageResource(mImageIds[position]);
    }

    public void onNothingSelected(AdapterView<?> parent) {
    }

    public View makeView() {
        ImageView i = new ImageView(this);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        return i;
    }

    private ImageSwitcher mSwitcher;

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageResource(mThumbIds[position]);
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            i.setBackgroundResource(R.drawable.empty_frame);
            return i;
        }

        private Context mContext;

    }

    private Integer[] mThumbIds = {
            R.drawable.dbyouth000, R.drawable.dbyouth001,
            R.drawable.dbyouth001b, R.drawable.dbyouth002};

    private Integer[] mImageIds = {
            R.drawable.dbyouth000, R.drawable.dbyouth001, R.drawable.dbyouth001b,
            R.drawable.dbyouth002};

}
<小时/>

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageSwitcher android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
    />

    <Gallery android:id="@+id/gallery"
        android:background="#55000000"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:gravity="center_vertical"
        android:spacing="16dp"
    />

</RelativeLayout>
<小时/>

感谢您的阅读。任何帮助/链接都将受到极大且不可撤销的赞赏:)

编辑:

<小时/>

我正在将此代码示例用于viewpager..如何在底部添加一个 TextView 来详细说明故事

请通过编辑此代码来解释..非常感谢:)

MainActivity.java

package com.manishkpr.viewpagerimagegallery;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this);
    viewPager.setAdapter(adapter);
  }


}
<小时/>

ImageAdapter.java

package com.manishkpr.viewpagerimagegallery;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
    Context context;
    private int[] GalImages = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three
    };
    ImageAdapter(Context context){
        this.context=context;
    }
    @Override
    public int getCount() {
      return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      ImageView imageView = new ImageView(context);
      int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(GalImages[position]);
      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }
<小时/>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

          <android.support.v4.view.ViewPager
          android:id="@+id/view_pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

</RelativeLayout>

此代码示例没有可用的缩略图,也没有用于指示滑动的左/右箭头。我不知道要实现这些。也许需要一些帮助?

最佳答案

你好!!

  1. 您尝试过 android ViewPager 组件吗?

    如果没有,请通过this .它是一个非常有用的组件。它 解决了你所有的问题。

    1 : http://developer.android.com/training/animation/screen-slide.html

  2. 如果画廊出现问题,请访问 HorizontalScrollView android 的组件。

关于java - 将滑动功能和 TextView 添加到图像幻灯片 Android 应用程序 - 漫画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25256273/

相关文章:

java - 使用 Actors/AKKA 的面向 session 的异步架构

java - 如何为createNewToken方法编写测试方法?

android - Volley 库使服务器无响应

java - 在 Android 中设置文本

java - 如何将 TextView 中的框架放入表格的形式?

android - 扩展 CardView 的最后一项

java - IntelliJ 格式化许可证头

java - 如何让一个函数每 2 个刻度被调用一次

android - NetworkInfo TYPE_MOBILE_MMS 功能

android - 由于异常 : System. MissingMethodException : UnityEditor. VersionControl.Task 而停止轮询作业