Saltar para conteúdo


Foto
- - - - -

CoverFlow em Galleria


  • Por favor inicie sessão para responder
5 respostas a este tópico

#1 HideCode

HideCode

    Membro

  • Membros
  • PipPip
  • 15 mensagens
  • htc Desire

Mensagem publicada 23 September 2013 - 09:57

Ola comunidade android, estou a fazer uma aplicação e queria colocar uma galeria na parte de cima do ecrã com o efeito de coverflow, mas não estou a conseguir fazer, ou seja, já consegui fazer uma aplicação só com o coverflow, não estou a conseguir é transportar este efeito para a galeria do meu projecto principal. e tambem não consigo meter elementos novos na aplicação do coverflow.

 

A aplicação coverFlow tem as seguintes classes

 

Classe de Activity


public class HomeActivity extends Activity{
	
	private  final static String TAG = "HomeActivity";
    private TextView pageNameTextView;
    private CoverFlow coverFlow;
    private ImageAdapter coverImageAdapter;
    private int itemSelected = 0;
    private Context context;
    private SparseArray<String> listeNomIcons;
    private int currentImagePosition = 0;

    //Info button
    private ImageView infoAccueilImageView; 

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
        //animate Transition
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

        context = this;

        listeNomIcons = new SparseArray<String>();
        listeNomIcons.put(0, "DELAIS D'ATTENTE, RETARD");
        listeNomIcons.put(1, "COURRIER SUBSTITUTION");
        listeNomIcons.put(2, "IR LC");
        listeNomIcons.put(3, "CONTACTS UTILES");
        listeNomIcons.put(4, "TEMPS DE PAUSE");
   

        coverFlow = (CoverFlow)findViewById(R.id.coverflow);  
        coverImageAdapter =  new ImageAdapter(this);  
        coverFlow.setAdapter(coverImageAdapter);
        coverFlow.setSelection(0, true);
        coverFlow.setAnimationDuration(1000);

        //cover

        pageNameTextView = (TextView)findViewById(R.id.page_nameTextView);

        coverFlow.setOnItemSelectedListener(new OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentImagePosition = position; //this will update your current marker
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        coverFlow.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                pageNameTextView.setText(listeNomIcons.get(arg2));
                itemSelected = arg2;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private final Context mContext;


        private final Integer[] mImageIds = {
                R.drawable.alipay,
                R.drawable.amazon_payments,
                R.drawable.american_express_gold,
                R.drawable.bank_of_america
           
        };

        private final ImageView[] mImages;

        public ImageAdapter(Context c) {
            mContext = c;
            mImages = new ImageView[mImageIds.length];
        }

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //Use this code if you want to load from resources
            ImageView i = new ImageView(mContext);
            i.setImageResource(mImageIds[position]);
            i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
            i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
            //Make sure we set anti-aliasing otherwise we get jaggies
            BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
            drawable.setAntiAlias(true);
            return i;

            //return mImages[position];
        }
        /** Returns the size (0.0f to 1.0f) of the views 
         * depending on the 'offset' to the center. */ 
        public float getScale(boolean focused, int offset) { 
            /* Formula: 1 / (2 ^ offset) */ 
            return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset))); 
        } 

    }
}

Agora vou colocar a classe do efeito Coverflow

public class CoverFlow extends Gallery {

    /**
     * Graphics Camera used for transforming the matrix of ImageViews
     */
    private final Camera mCamera = new Camera();

    /**
     * The maximum angle the Child ImageView will be rotated by
     */    
    private int mMaxRotationAngle = 80;

    /**
     * The maximum zoom on the centre Child
     */
    // TODO RENDRE LA VALEUR DYNAMIQUE SUR LA TAILLE DE L'ECRAN

//    private int mMaxZoom = -430;
    private int mMaxZoom = -370;

    /**
     * The Centre of the Coverflow 
     */   
    private int mCoveflowCenter;

 public CoverFlow(Context context) {
  super(context);
  this.setStaticTransformationsEnabled(true);
 }

 public CoverFlow(Context context, AttributeSet attrs) {
  super(context, attrs);
        this.setStaticTransformationsEnabled(true);
 }

  public CoverFlow(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   this.setStaticTransformationsEnabled(true);   
  }

    /**
     * Get the max rotational angle of the image
  * @return the mMaxRotationAngle
  */
 public int getMaxRotationAngle() {
  return mMaxRotationAngle;
 }

 /**
  * Set the max rotational angle of each image
  * @param maxRotationAngle the mMaxRotationAngle to set
  */
 public void setMaxRotationAngle(int maxRotationAngle) {
  mMaxRotationAngle = maxRotationAngle;
 }

 /**
  * Get the Max zoom of the centre image
  * @return the mMaxZoom
  */
 public int getMaxZoom() {
  return mMaxZoom;
 }

 /**
  * Set the max zoom of the centre image
  * @param maxZoom the mMaxZoom to set
  */
 public void setMaxZoom(int maxZoom) {
  mMaxZoom = maxZoom;
 }

 /**
     * Get the Centre of the Coverflow
     * @return The centre of this Coverflow.
     */
    private int getCenterOfCoverflow() {
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
    }

    /**
     * Get the Centre of the View
     * @return The centre of the given view.
     */
    private static int getCenterOfView(View view) {
        return view.getLeft() + view.getWidth() / 2;
    }  
    /**
  * {@inheritDoc}
  *
  * @see #setStaticTransformationsEnabled(boolean) 
  */ 
    @Override
    protected boolean getChildStaticTransformation(View child, Transformation t) {

  final int childCenter = getCenterOfView(child);
  final int childWidth = child.getWidth() ;
  int rotationAngle = 0;

  t.clear();
  t.setTransformationType(Transformation.TYPE_MATRIX);

        if (childCenter == mCoveflowCenter) {
            transformImageBitmap((ImageView) child, t, 0);
        } else {      
            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter)/ childWidth) *  mMaxRotationAngle);
            if (Math.abs(rotationAngle) > mMaxRotationAngle) {
             rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;   
            }
            transformImageBitmap((ImageView) child, t, rotationAngle);         
        }    

  return true;
 }

 /**
  * This is called during layout when the size of this view has changed. If
  * you were just added to the view hierarchy, you're called with the old
  * values of 0.
  *
  * @param w Current width of this view.
  * @param h Current height of this view.
  * @param oldw Old width of this view.
  * @param oldh Old height of this view.
     */
     @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
      mCoveflowCenter = getCenterOfCoverflow();
      super.onSizeChanged(w, h, oldw, oldh);
     }

     /**
      * Transform the Image Bitmap by the Angle passed 
      * 
      * @param imageView ImageView the ImageView whose bitmap we want to rotate
      * @param t transformation 
      * @param rotationAngle the Angle by which to rotate the Bitmap
      */
     private void transformImageBitmap(ImageView child, Transformation t, int rotationAngle) {            
      mCamera.save();
      final Matrix imageMatrix = t.getMatrix();;
      final int imageHeight = child.getLayoutParams().height;;
      final int imageWidth = child.getLayoutParams().width;
      final int rotation = Math.abs(rotationAngle);

      mCamera.translate(0.0f, 0.0f, 100.0f);

      //As the angle of the view gets less, zoom in     
      if ( rotation < mMaxRotationAngle ) {
       float zoomAmount = (mMaxZoom + rotation);
       mCamera.translate(0.0f, 0.0f, zoomAmount);          
      } 

      mCamera.rotateY(rotationAngle);
      mCamera.getMatrix(imageMatrix);               
      imageMatrix.preTranslate(-(imageWidth/2), -(imageHeight/2)); 
      imageMatrix.postTranslate((imageWidth/2), (imageHeight/2));
      mCamera.restore();
 }
}

E o meu XML onde não consigo ver o TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent" >
	
    <com.example.coverflow.CoverFlow
	android:id="@+id/coverflow"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"/>

    
    <ImageView
        android:id="@+id/infoImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_alert" />

    <TextView
        android:id="@+id/page_nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

Alguém me consegue ajudar a transportar este efeito para a minha aplicação principal e a colocar a galeria na parte de cima do ecrã.

 

Obrigado

 



#2 pedronveloso

pedronveloso

    Veloso

  • Administradores
  • 1578 mensagens
  • S8

Mensagem publicada 03 October 2013 - 22:35

Estão aí grandes blocos de código, é complicado imaginar o que possa estar errado. Se ainda não resolveste o problema, se puderes partilhar o código do projecto posso montar o mesmo do meu lado e tentar aí sim perceber o que se passa.



#3 HideCode

HideCode

    Membro

  • Membros
  • PipPip
  • 15 mensagens
  • htc Desire

Mensagem publicada 08 October 2013 - 13:32

Depois de muito experimentar lá consegui colocar isto a funcionar, obrigado :)


  • pedronveloso gosta disto

#4 HideCode

HideCode

    Membro

  • Membros
  • PipPip
  • 15 mensagens
  • htc Desire

Mensagem publicada 12 December 2013 - 18:44

Estou com um novo problema podes ajudar-me?



#5 pedronveloso

pedronveloso

    Veloso

  • Administradores
  • 1578 mensagens
  • S8

Mensagem publicada 05 January 2014 - 17:15

Estou com um novo problema podes ajudar-me?

 

Explica qual é o novo problema ;). (se ainda for aplicável)



#6 HideCode

HideCode

    Membro

  • Membros
  • PipPip
  • 15 mensagens
  • htc Desire

Mensagem publicada 06 January 2014 - 09:39

O problema é que quando a imagem se move para o lado não consigo que o titulo dela tambem se mova, ou seja se carregar na imagem e deslizar para o lado o nome o titulo tambem muda, agora se deslizar só ai o titulo não se altera.

 

Obrigado