Android實現圖片滾動效果
阿新 • • 發佈:2020-09-18
Android開發圖片滾動效果,供大家參考,具體內容如下
效果圖:
設定適配來設定圖片位置大小
package com.example.gallary; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context mContext; // 圖片陣列源 private Integer[] imgs = { R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,R.drawable.img7}; public ImageAdapter(Context c) { mContext = c; } @Override public int getCount() { return imgs.length; } // 獲取圖片位置 @Override public Object getItem(int position) { return imgs[position]; } // 獲取圖片ID @Override public long getItemId(int position) { return position; } @Override public View getView(int position,View convertView,ViewGroup parent) { ImageView imageview = new ImageView(mContext); imageview.setImageResource(imgs[position]); imageview.setLayoutParams(new Gallery.LayoutParams(240,200)); // 設定佈局 圖片120×120顯示 imageview.setScaleType(ImageView.ScaleType.CENTER); // 設定顯示比例型別(不縮放) return imageview; } }
main新增圖片資源
package com.example.gallary; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Gallery gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); // gallery新增ImageAdapter圖片資源 } }
佈局
<TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center" android:layout_marginTop="50dip" android:textColor="#ffff0000" android:textSize="30sp" android:text="滾動圖片"/> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:layout_below="@id/tv" />
drawable放置圖片資源
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。