1
for(String name : Hash_file_maps.keySet()){

            TextSliderView textSliderView = new TextSliderView(MainActivity.this);
            textSliderView
                    .description(name)
                    .image()
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener(this);
            textSliderView.bundle(new Bundle());
            textSliderView.getBundle()
                    .putString("extra",name);
            sliderLayout.addSlider(textSliderView);
        }
       // sliderLayout.setPresetTransformer(SliderLayout.Transformer.A);
        sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        sliderLayout.setCustomAnimation(new DescriptionAnimation());
        sliderLayout.setDuration(2000);
        sliderLayout.addOnPageChangeListener(this);
    }

el .image() me pide un url pero en mi caso tengo un mapa de bits, y no se como mostrar mis imágenes en el carrusel.

3
  • utilizo un Hash_file_maps Commented el 20 jun. 2017 a las 19:21
  • Hash_file_maps es HashMap que contiene las url. es.stackoverflow.com/questions/78588/… puedes convertir esta imagen a bitmap pero el método solo acepta una url, ya revisaste esto?
    – Jorgesys
    Commented el 20 jun. 2017 a las 19:46
  • ¿Que tiene que ver este problema con la etiqueta 'android-studio'? Commented el 5 jul. 2017 a las 13:18

1 respuesta 1

0

Para cargar una imagen se usa el método .image() dentro de este el parámetro necesario es precisamente una url.

Si estas usando la clase TextSliderView , tendrías que realizar tu mismo la implementación.

De hecho ya se ha requerido esto.. https://github.com/daimajia/AndroidImageSlider/issues/130

pero no se ha agregado, entonces como solución es modificar la clase BaseSlider para que puedas asignar al método image() un bitmap :

package com.daimajia.slider.library.SliderTypes;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.daimajia.slider.library.R;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;

import java.io.File;

/**

When you want to make your own slider view, you must extends from this class.

BaseSliderView provides some useful methods.

I provide two example: {@link com.daimajia.slider.library.SliderTypes.DefaultSliderView} and

{@link com.daimajia.slider.library.SliderTypes.TextSliderView}

if you want to show progressbar, you just need to set a progressbar id as @+id/loading_bar.
*/
public abstract class BaseSliderView {

protected Context mContext;

private Bundle mBundle;

/**

Error place holder image.
*/
private int mErrorPlaceHolderRes;
/**

Empty imageView placeholder.
*/
private int mEmptyPlaceHolderRes;
private String mUrl;
private File mFile;
private int mRes;
private Bitmap mBitmap;

protected OnSliderClickListener mOnSliderClickListener;

private boolean mErrorDisappear;

private ImageLoadListener mLoadListener;

private String mDescription;

/**

Scale type of the image.
*/
private ScaleType mScaleType = ScaleType.Fit;
public enum ScaleType{
CenterCrop, CenterInside, Fit, FitCenterCrop
}

protected BaseSliderView(Context context) {
mContext = context;
this.mBundle = new Bundle();
}

/**

the placeholder image when loading image from url or file.
@Param resId Image resource id
@return
*/
public BaseSliderView empty(int resId){
mEmptyPlaceHolderRes = resId;
return this;
}
/**

determine whether remove the image which failed to download or load from file
@Param disappear
@return
*/
public BaseSliderView errorDisappear(boolean disappear){
mErrorDisappear = disappear;
return this;
}
/**

if you set errorDisappear false, this will set a error placeholder image.
@Param resId image resource id
@return
*/
public BaseSliderView error(int resId){
mErrorPlaceHolderRes = resId;
return this;
}
/**

the description of a slider image.
@Param description
@return
*/
public BaseSliderView description(String description){
mDescription = description;
return this;
}
/**

set a url as a image that preparing to load
@Param url
@return
*/
public BaseSliderView image(String url){
if(mFile != null || mRes != 0 || mBitmap != null){
throw new IllegalStateException("Call multi image function," +
"you only have permission to call it once");
}
mUrl = url;
return this;
}
/**

set a file as a image that will to load
@Param file
@return
*/
public BaseSliderView image(File file){
if(mUrl != null || mRes != 0 || mBitmap != null){
throw new IllegalStateException("Call multi image function," +
"you only have permission to call it once");
}
mFile = file;
return this;
}
public BaseSliderView image(int res){
if(mUrl != null || mFile != null || mBitmap != null){
throw new IllegalStateException("Call multi image function," +
"you only have permission to call it once");
}
mRes = res;
return this;
}

public BaseSliderView image(Bitmap bitmap){
if(mUrl != null || mFile != null || mBitmap != null){
throw new IllegalStateException("Call multi image function," +
"you only have permission to call it once");
}
mBitmap = bitmap;
return this;
}

public String getUrl(){
return mUrl;
}

public boolean isErrorDisappear(){
return mErrorDisappear;
}

public int getEmpty(){
return mEmptyPlaceHolderRes;
}

public int getError(){
return mErrorPlaceHolderRes;
}

public String getDescription(){
return mDescription;
}

public Context getContext(){
return mContext;
}

/**

set a slider image click listener
@Param l
@return
*/
public BaseSliderView setOnSliderClickListener(OnSliderClickListener l){
mOnSliderClickListener = l;
return this;
}
/**

When you want to implement your own slider view, please call this method in the end in getView() method

@Param v the whole view

@Param targetImageView where to place image
*/
protected void bindEventAndShow(final View v, ImageView targetImageView){
final BaseSliderView me = this;

v.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
if(mOnSliderClickListener != null){
mOnSliderClickListener.onSliderClick(me);
}
}
});

mLoadListener.onStart(me);

Picasso p = Picasso.with(mContext);
RequestCreator rq = null;
if(mUrl!=null){
rq = p.load(mUrl);
}else if(mFile != null){
rq = p.load(mFile);
}else if(mBitmap != null){
targetImageView.setImageBitmap(mBitmap);
if(v.findViewById(R.id.loading_bar) != null){
v.findViewById(R.id.loading_bar).setVisibility(View.INVISIBLE);
}
}else if(mRes != 0){
rq = p.load(mRes);
}else{
return;
}

if(rq == null){
return;
}

if(getEmpty() != 0){
rq.placeholder(getEmpty());
}

if(getError() != 0){
rq.error(getError());
}

switch (mScaleType){
case Fit:
rq.fit();
break;
case CenterCrop:
rq.fit().centerCrop();
break;
case CenterInside:
rq.fit().centerInside();
break;
}

rq.into(targetImageView,new Callback() {
@override
public void onSuccess() {
if(v.findViewById(R.id.loading_bar) != null){
v.findViewById(R.id.loading_bar).setVisibility(View.INVISIBLE);
}
}

@Override
public void onError() {
    if(mLoadListener != null){
        mLoadListener.onEnd(false,me);
    }
}
});
}

public BaseSliderView setScaleType(ScaleType type){
mScaleType = type;
return this;
}

public ScaleType getScaleType(){
return mScaleType;
}

/**

the extended class have to implement getView(), which is called by the adapter,
every extended class response to render their own view.
@return
*/
public abstract View getView();
/**

set a listener to get a message , if load error.
@Param l
*/
public void setOnImageLoadListener(ImageLoadListener l){
mLoadListener = l;
}
public interface OnSliderClickListener {
public void onSliderClick(BaseSliderView slider);
}

/**

when you have some extra information, please put it in this bundle.
@return
*/
public Bundle getBundle(){
return mBundle;
}
public interface ImageLoadListener{
public void onStart(BaseSliderView target);
public void onEnd(boolean result,BaseSliderView target);
}

}
2
  • ya vi gracias, pero como llamo a la clase y la utilizo en vez de mi codigo Commented el 20 jun. 2017 a las 21:13
  • cree un nueva clase con el codigo que me mandaste @Jorgesys llamada Carrucel, pero al momento de enviarle sliderLayout.addSlider(car); no me funciona me da error, ayuda xfavor, es que soy estudiante Commented el 21 jun. 2017 a las 4:35

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.