Karussell mit Homepage-Bildern
/** * Automatische Rotationssteuerung für Werbebilder</br> * */public class ImageCycleView erweitert LinearLayout { /** * Context*/ private Context mContext; ** * Anpassung der Bildlaufansicht*/ private ImageCycleAdapter mAdvAdapter; /** * Bildrotationsanzeigesteuerung*/ private ViewGroup mGroup; Bildkarussell zeigt jedes Bild an */ private ImageView mImageView = null; /** * Bildlauf-Anweisungsliste */ private ImageView[] mImageViews = null; /** * Bild scrollt aktuelles Bildsubskript */ private boolean isStop; * Ob der Cursor kreisförmig oder lang ist. Wenn er auf 1 gesetzt ist, ist er kreisförmig. */ public int stype=1; param context */ public ImageCycleView(Context context) { super(context); } /** * @param context * @param attrs */ @SuppressLint("Recycle") public ImageCycleView(Context context, AttributeSet attrs) { super(context, attrs); = context; LayoutInflater.from(context).inflate(R.layout.ad_cycle_view, this); (ViewPager) findViewById(R.id.adv_pager); mAdvPager.setOnPageChangeListener(new GuidePageChangeListener()); // Scrollen Sie durch den unteren rechten Indikator des Bildes mGroup = (ViewGroup) findViewById(R.id.viewGroup); * Touch stoppt den Timer, Lift startet den Timer*/ @Override public boolean DispatchTouchEvent(MotionEvent Event) { if(event.getAction()==MotionEvent.ACTION_UP){ // Bildlauf starten startImageTimerTask(); }else{ // Bildlauf stoppen stopImageTimerTask( } return super.dispatchTouchEvent(event); } /** * Bilddaten laden * * @param imageUrlList * @param imageCycleViewListener */ public void setImageResources(ArrayList<Integer> imageUrlList,ImageCycleViewListener imageCycleViewListener,int stype){ this.stype=stype; // Clear mGroup.removeAllViews(); // Anzahl der Bildanzeigen final int imageCount = imageUrlList.size(); mImageViews = new ImageView[imageCount]; for (int i = 0; i < imageCount; i++) { mImageView = new ImageView(mContext); LayoutParams=new LayoutParams.WRAP_CONTENT; params.leftMargin=30; mImageView.setLayoutParams(params); mImageViews[i] = mImageView; if (i == 0) { if(this.stype==1)// mImageViews[i].setBackgroundResource(R.drawable.banner_dian_focus); mImageViews[i].setBackgroundResource(R.drawable .cicle_banner_dian_focus);//Zu else wechseln mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_focus); } else { if(this.stype==1) // mImageViews[i].setBackgroundResource(R.drawable.banner_dian_blur); mImageViews[i].setBackgroundResource(R .drawable.cicle_banner_dian_blur); sonst mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); } mGroup.addView(mImageViews[i]); } mAdvAdapter = new ImageCycleAdapter(mContext, imageUrlList,imageCycleViewListener); mAdvPager.setCurrentItem(Integer.MAX_VALUE/2); startImageTimerTask(); } public void setImageResources2(ArrayList<Integer> imageUrlList ,ImageCycleViewListener,int stype){ this.stype=stype; // Clear mGroup.removeAllViews(); Anzahl der Image-Anzeigen final int imageCount = imageUrlList(); mImageViews = new ImageView[imageCount]; for (int i = 0; i < imageCount; i++) { mImageView = new ImageView(mContext); LayoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams. WRAP_CONTENT); params.leftMargin=30; mImageView.setScaleType(ScaleType.CENTER_CROP); mImageView.setLayoutParams(params); mImageViews[i] = mImageView; if (this.stype==1)// mImageViews[i].setBackgroundResource(R .drawable.banner_dian_focus); mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_focus);//Change else mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_focus); } else { if(this.stype==1) // mImageViews[i ] .setBackgroundResource(R.drawable.banner_dian_blur); mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); else mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); } mGroup.addView(mImageViews[i]); } mAdvAdapter = new ImageCycleAdapter(mContext, imageUrlList ,imageCycleViewListener); mAdvPager.setCurrentItem(Integer.MAX_VALUE/2); } /** * Bildkarussell (manuelle Steuerung des automatischen Karussells oder nicht, praktisch für die Ressourcensteuerung) void startImageCycle() { startImageTimerTask(); } /** * Karussell anhalten – wird verwendet, um Ressourcen zu sparen*/ public void pushImageCycle() { stopImageTimerTask(); } /** * Bildlaufaufgabe*/ private void startImageTimerTask(); // Bildlaufaufgabe mHandler.postDelayed(mImageTimerTask, 5000 ); } /** * Bildlaufaufgabe stoppen*/ private void stopImageTimerTask() { isStop=true; mHandler.removeCallbacks(mImageTimerTask); } private Handler mHandler = new Handler(); /** * Automatische Bildrotationsaufgabe */ private Runnable() { @Override public void run ( ) { if (mImageViews != null) { mAdvPager.setCurrentItem(mAdvPager.getCurrentItem()+1); if(!isStop){ //if isStop=true //Wenn Sie beenden, müssen Sie dies stoppen, sonst existiert es weiterhin und führt eine Schleife im Hintergrund-mHandler durch. postDelayed( mImageTimerTask, 5000); } } } }; /** * Karussellbildüberwachung* * @author minking */ privat Die letzte Klasse GuidePageChangeListener implementiert OnPageChangeListener { @Override public void onPageScrollStateChanged(int state) { if (state == ViewPager.SCROLL_STATE_IDLE) startImageTimerTask(); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int index) { index=index%mImageViews.length; // Setzt das aktuell angezeigte Bild // Setzt den Bildlaufindikator zurück if(stype!=1) mImageViews[index].setBackgroundResource(R.drawable.cicle_banner_dian_focus); .setBackgroundResource(R.drawable.cicle_banner_dian_focus); for (int i = 0; i < mImageViews.length; i++) { if (index != i) { if(stype!=1) mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); else mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur) ; } } } } private Klasse ImageCycleAdapter erweitert PagerAdapter { /** * Bildansicht-Cache-Liste*/ private ArrayList<SmartImageView> mImageViewCacheList; /** * Bildressourcenliste*/ //private ArrayList<String>(); private ArrayList<Integer> mAdList = new ArrayList<Integer>(); /** * Anzeigenbild-Klicküberwachung*/ private ImageCycleViewListener mImageCycleViewListener; privater Kontext mContext;// public ImageCycleAdapter(Context context, ArrayList<String> adList, ImageCycleViewListener) {// this.mAdList = adList; // mImageCycleViewListener = neu ArrayList<SmartImageView>();// } public ImageCycleAdapter(Context context, ArrayList<Integer> adList , ImageCycleViewListener imageCycleViewListener) { this.mContext = context; this.mAdList = adList; mImageCycleViewListener = imageCycleViewListener; mImageViewCacheList = new ArrayList<SmartImageView>( ); } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object obj) { return view == obj } @Override public Object instantiateItem(ViewGroup container, final int position) { int imageUrl = mAdList .get(position%mAdList.size()); Log.e("imageUrl:",position%mAdList()+"--"+imageUrl+""); SmartImageView imageView = null; .setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); //test imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); .setImageUrl(imageUrl); imageView.setBackgroundResource(imageUrl); //Bildklick-Listener festlegen imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mImageCycleViewListener.onImageClick(position%mAdList.size(), v); } }); public void destroyItem(ViewGroup container, int position, Object object) { SmartImageView view = (SmartImageView) object; mAdvPager.removeView(view); mImageViewCacheList.add(view); } } /** * Listening-Ereignisse für das Karussell-Steuerelement* * @author minking */ public static interface ImageCycleViewListener { /** * Klicken Sie auf das Bild Ereignis * * @param position * @param imageView */ public void onImageClick(int position, View imageView);
Werfen wir einen Blick auf ein Bildkarussell-Steuerelement basierend auf Android ViewPager
package me.lanfog.myandroid.widget; import java.util.ArrayList;import java.content.Context;import android.os.Handler;import android.os.Message;import android.support.v4 .view.PagerAdapter;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.widget.ImageView; public class PageFlipper erweitert ViewPager { private String TAG = PageFlipper.class.getSimpleName(); private List<View> Ansichten; privater PagerAdapter-Adapter = neuer PagerAdapter() { @Override public Object instantiateItem(ViewGroup-Container, int Position) { View v = views.get(position); container.addView(v); return v; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public int getItemPosition(Object object) { return view.indexOf(object); } @Override public void destroyItem(ViewGroup container, int position, Object object) { Container.RemoveView((View)Object); Konvertieren Sie die Kontrollposition in die Position im Datensatz*/ public int Convert(int Position){ Return Position == 0 ? Views.size()-1 : ( Position > Views.Size() ? 0: position-1); } @Override public void onPageSelected(int position) { if(listener2 != null){ listener2.onPageSelected(convert(position)); int offset) { if(listener2 != null){ listener2.onPageScrolled(convert(position), percent, offset } if(percent == 0){ if(position == 0) // Zur vorletzten Seite wechseln setCurrentItem(( views.size() - 2 ) % views.size(), false); .size() - 1) // Zur zweiten positiven Seite wechseln setCurrentItem(1, false); != null){ listener2.onPageScrollStateChanged(state); } switch (state) { case SCROLL_STATE_IDLE: // Leerlauf if(!handler.hasMessages(START_FLIPPING)) handler.sendEmptyMessageDelayed(START_FLIPPING, 3000); // Verzögerung beim Scrollen; SCROLL_STATE_DRAGGING: // handler.sendEmptyMessage(STOP_FLIPPING); // Scrollen abbrechen; case SCROLL_STATE_SETTLING: // Ziehen endet; private final int STOP_FLIPPING = 1; private Handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case START_FLIPPING: if(views.size() > 3) // Da die Vorder- und Rückseite Hilfsseiten sind, bedeutet 3 hier nur 1 Seite setCurrentItem((getCurrentItem() + 1) % view.size ()); handler.sendEmptyMessageDelayed(START_FLIPPING, 3000); // Verzögerte Scroll-Pause STOP_FLIPPING: handler.removeMessages(START_FLIPPING); public PageFlipper(Context, AttributeSet) { super(context, attrs); ; } private void init(){ setOffscreenPageLimit(1); Maximale Seiten-Cache-Nummer setAdapter(adapter); // Adapter super.setOnPageChangeListener(listener); // Verzögerter Bildlauf} public void setViews(int[] ids){ this new ArrayList<View>(); for(int i=0;i<ids.length+2;i++){ // Eine letzte Seite am Kopf und eine Homepage am Ende hinzufügen. iv = new ImageView(getContext()); iv.setImageResource(i == 0 ? ids.length-1 : ( i > ids. length ? 0 : i-1 )]); iv.setScaleType(ImageView.ScaleType.FIT_XY); setCurrentItem(1); // Startseite this.adapter.notifyDataSetChanged(); } @Override public void setOnPageChangeListener) { this.listener2 = listener;