2016-02-12 195 views
0

我有這個代碼刷卡,我有一個字符串許多圖像[]幻燈片效果的Android

公共類SlideShowUserImages擴展ImageView的實現TargetLoaded {

private String[] urls; 

private int curPos = 0; 

private int imagesSize = 0; 

private GestureDetector gdt; 

private TargetImageView nextImageView; 

private Dot[] dots; 

//CANVAS 
private int radius = 10; 

private int dotMargin = 5; 

private int dotYPos = -1; 


private Paint selectedPaint; 

private Paint normalPaint; 


public SlideShowUserImages(Context context) { 
    super(context); 
    init(context, null); 
} 


public SlideShowUserImages(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context, attrs); 
} 

public SlideShowUserImages(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(context, attrs); 
} 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public SlideShowUserImages(Context context, AttributeSet attrs, int defStyleAttr, 
        int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    init(context, attrs); 
} 


private void init(Context context, AttributeSet attrs) { 
    gdt = new GestureDetector(context, new GestureListener()); 
    nextImageView = new TargetImageView(this); 
    setClickable(true); 

    this.normalPaint = new Paint(); 
    this.selectedPaint = new Paint(); 
    this.normalPaint.setStyle(Paint.Style.FILL); 
    this.selectedPaint.setStyle(Paint.Style.FILL); 

    int dotNormalColor = Color.WHITE; 
    int dotSelectedColor = Color.GRAY; 

    TypedArray a = context.getTheme().obtainStyledAttributes(
      attrs, 
      R.styleable.SlideShow, 
      0, 0); 

    try { 
     dotNormalColor = a.getColor(R.styleable.SlideShow_dotNormal, Color.WHITE); 
     dotSelectedColor = a.getColor(R.styleable.SlideShow_dotSelected, Color.GRAY); 
     radius = a.getDimensionPixelSize(R.styleable.SlideShow_dotRadius, 10); 
     dotMargin = a.getDimensionPixelSize(R.styleable.SlideShow_dotMargin, 5); 
    } finally { 
     a.recycle(); 
    } 

    this.selectedPaint.setColor(dotSelectedColor); 
    this.normalPaint.setColor(dotNormalColor); 

    this.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      return gdt.onTouchEvent(event); 
     } 
    }); 
} 

public void start(String[] urls) { 
    this.urls = urls; 
    this.imagesSize = this.urls.length; 

    // setup dots 
    dots = new Dot[imagesSize]; 
    int dotY = 2 * radius - dotMargin; 
    int dotWidth = 2 * radius + dotMargin; 
    for (int i = 0; i < dots.length; i++) { 
     dots[i] = new Dot((imagesSize/2 - i) * dotWidth, dotY, radius); 
    } 

    getImageIntoView(0); 
} 


private void getImageIntoView(int position) { 

    Picasso.with(getContext()).load(urls[position]).placeholder(R.drawable.ic_amigos).into(nextImageView); 
} 


protected void next() { 
    curPos++; 
    if (curPos > imagesSize - 1) { 
     curPos = 0; 
    } 
    getImageIntoView(curPos); 
} 


protected void prev() { 
    curPos--; 
    if (curPos < 0) { 
     curPos = imagesSize - 1; 
    } 
    getImageIntoView(curPos); 
} 

@Override 
public void onTargetLoaded(Bitmap bitmap) { 
    setImageBitmap(bitmap); 
    invalidate(); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    if (dots == null) { 
     return; 
    } 

    int offsetX = canvas.getWidth()/2; 
    int offsetY = canvas.getHeight(); 
    for (int i = 0; i < dots.length; i++) { 
     dots[i].render(canvas, offsetX, offsetY, (curPos == i) ? selectedPaint : normalPaint); 
    } 

} 

private class Dot { 

    private int posX, posY, radius; 

    public Dot() { 

    } 

    public Dot(int posX, int posY, int radius) { 
     this.posX = posX; 
     this.posY = posY; 
     this.radius = radius; 
    } 

    public void render(Canvas canvas, int offsetX, int offsetY, Paint paint) { 
     /*canvas 
       .drawCircle(offsetX - posX, offsetY - posY, radius, paint);*/ 
    } 
} 

private class GestureListener extends GestureDetector.SimpleOnGestureListener { 

    private final int SWIPE_MIN_DISTANCE = 100; 

    private final int SWIPE_THRESHOLD_VELOCITY = 70; 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
       && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
      // Right to left, your code here 
      next(); 
      return true; 
     } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
       && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
      // Left to right, your code here 
      prev(); 
      return true; 
     } 
     return false; 
    } 
} 


public class TargetImageView implements Target { 

    TargetLoaded targetLoaded; 

    public TargetImageView(TargetLoaded targetLoaded) { 
     this.targetLoaded = targetLoaded; 
    } 

    @Override 
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
     targetLoaded.onTargetLoaded(bitmap); 
    } 

    @Override 
    public void onBitmapFailed(Drawable errorDrawable) { 

    } 

    @Override 
    public void onPrepareLoad(Drawable placeHolderDrawable) { 

    } 
} 

}

但這改變了圖像像閃爍和動畫必須慢

回答

1

我認爲你正在尋找視圖尋呼機。

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

而在你的活動中,你可以設置一個適配器到你的viewpager。

viewPager = (ViewPager) findViewById(R.id.pager); 
viewPager.setAdapter(adapter); 

其中adapter是Class Adapter的對象,可以通過擴展PagerAdapter來定義。

您可以設置圖像適配器的以下方法內部類

@Override 
public Object instantiateItem(ViewGroup container, int position){ 
View view; 
LayoutInflater inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE; 
view=inflater.inflate(R.layout.your_layout,container,false); 

ImageView imageview = (ImageView)view. findViewById(R.id.your_image); 

//now set your image to the imageView 
}