M
M
mr-zherart2014-03-17 01:46:13
Android
mr-zherart, 2014-03-17 01:46:13

How to implement image slider for android app?

Hello. There is an application which has selector.xml and SelectorActivity.java.
Selector.xml - layout with two buttons and an image. The picture occupies half the height of the screen and the button half the height of the screen in a row.
SelectorActivity - responds to button presses and loads an image from assets.
You need to make a touch image slider. I've tried all the options, but I can't seem to get it done.
Please suggest a solution. I myself am a beginner in application development (everything is at the level of a scientific poke).
SelectorActivity (initialPath - the path where the files come from)

public class SelectorActivity extends SherlockActivity {
  
  TextView text;

  private String initialPath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        
    	Intent intent = getIntent();
        initialPath = intent.getStringExtra(MainConf.INTENT_EXTRA_QRCODE);
    }
    
    @Override
    public void onStart(){
    	super.onStart();
    setContentView(R.layout.selector);
        setTitle(R.string.app_name);
        invalidateOptionsMenu();
        text = (TextView)findViewById(R.id.textView1);
        text.setText(initialPath);
        // load image
        try {
            // get input stream
        	String path =  MainConf.getPath(initialPath)+"/pict1.jpg";
            InputStream ims = getApplicationContext().getResources().getAssets().open(path);
            // load image as Drawable
            Drawable d = Drawable.createFromStream(ims, null);
            // set image to ImageView
            ImageView mImage = (ImageView)findViewById(R.id.picture_view);
            mImage.setImageDrawable(d);
        }
        catch(IOException ex) {
            Log.e(MainConf.TAG, ex.getMessage());
        }
        Log.d(MainConf.TAG, "onStart");
    }
 	 
    public void readBtnClick(View view){
    Intent intent = new Intent(this, ReadActivity.class);
    intent.putExtra(MainConf.INTENT_EXTRA_FULLPATH, initialPath);
        startActivity(intent);
  }
   
  public void hearBtnClick(View view){
    Intent intent = new Intent(this, HearActivity.class);
    intent.putExtra(MainConf.INTENT_EXTRA_FULLPATH, initialPath);
        startActivity(intent);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
   
  @Override
  public boolean onOptionsItemSelected(MenuItem item){
    MainConf.onOptionsItemSelected(this, item);
    return true;
  }
}

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/qr_text" />

<ImageView
    android:id="@+id/picture_view"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="9"
    android:baselineAlignBottom="false"
    android:cropToPadding="true"
    android:scaleType="fitCenter"
    android:scrollbarAlwaysDrawVerticalTrack="false"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="9"
    android:orientation="horizontal" >

  <Button
      android:id="@+id/readBtn"
      android:layout_width="0dip"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/read_btn_selector"
      android:onClick="readBtnClick"
      android:text="@string/read_btn"
      android:textColor="#ffffff" />
  
  <Button
      android:id="@+id/hearingBtn"
      android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="1"
      android:background="@drawable/hear_btn_selector"
      android:onClick="hearBtnClick"
      android:text="@string/hear_btn"
      android:textColor="#ffffff" />


</LinearLayout>

</LinearLayout>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JaLoveAst1k, 2014-03-18
@mr-zherart

Instead of ImageView you need to use some other component, I recently implemented this with two different ones:
HorizontalListView - a normal scrolling list, only horizontal. For it, you need to create an adapter by inheriting, for example, BaseAdapter and overriding its methods.
ViewPager - but it displays only 1 object at a time, i.e. if you stop the swipe in the middle, it will scroll to the nearest element, most likely you need it. The link has an example of use, but it is for fragments. It will be easier for you to make an adapter based on PagerAdapter, it is not much different from the same adapter for a sheet.

E
eremeevdev, 2014-03-20
@eremeevdev

If I understand correctly, then you need a ViewPager. See how it's done here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question