如何在android中显示Vertical Cover Flow

时间:2014-07-24 09:52:58

标签: coverflow

我想在我的应用程序中实现垂直封面流程。我有CoverFlow的源代码,但它显示水平方式。我在代码中做了一些更改并应用此方法imageView.setRotation(270)之后显示垂直模式但是一个问题是,它只显示封面流中心的一个图像,但它看起来不像Cover Flow。任何人都可以告诉我在哪里需要更改我的代码以查找垂直CoverFlow。您的帮助非常感谢。我很感激。在这里附上源代码

 public class AllDeals extends BaseFragment implements OnClickListener 
{

     CoverFlow cover;
     Button btn_back,btn_up,btn_down,btn_more;
     TextView txt_cardesc,txt_carprize,txt_vat;
     public   int myposition=-1;

    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        //return super.onCreateView(inflater, container, savedInstanceState);

        View v=inflater.inflate(R.layout.alldeals, container,false);
        cover=(CoverFlow)v.findViewById(R.id.alldeal_cover);

        btn_back=(Button)v.findViewById(R.id.btn_alldeal_back);
        btn_back.setOnClickListener(this);

        btn_up=(Button)v.findViewById(R.id.btn_alldeals_up);
        btn_down=(Button)v.findViewById(R.id.btn_all_deal_down);

        btn_more=(Button)v.findViewById(R.id.btn_all_deal_more);

        // for text views
        txt_cardesc=(TextView)v.findViewById(R.id.txt_cardesc);
        txt_carprize=(TextView)v.findViewById(R.id.txt_alldeal_carprize);
        txt_vat=(TextView)v.findViewById(R.id.txt_alldeal_vat);



        // add set action listener to buttons 

        btn_up.setOnClickListener(this);
        btn_down.setOnClickListener(this);
        btn_more.setOnClickListener(this);



        cover.setAdapter(new ImageAdapter(getActivity()));
        ImageAdapter coverImageAdapter = new ImageAdapter(getActivity());

        cover.setSpacing(-5);
        cover.setSelection(-1, true);
        cover.setAnimationDuration(1000);
        cover.setRotation(90);

        //set up listener for cover
        setListener(cover);

        return v;

    }
    private void setListener(CoverFlow myCoverFlow)
   {
        // TODO Auto-generated method stub
        myCoverFlow.setOnItemClickListener(new OnItemClickListener() 
        {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,
                    long arg3) 
            {
                // TODO Auto-generated method stub
                myposition=position;
            }

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

            @Override
            public void onItemSelected(AdapterView<?> arg0, View view,
                    int position, long arg3) 
            {
                // TODO Auto-generated method stub
                myposition=position;
            }

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

            }
        });


    }
    class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        private Integer[] mImageIds = { R.drawable.image01, R.drawable.image02,
                R.drawable.image03, R.drawable.image04, R.drawable.image05 };

        private ImageView[] mImages;

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

        @SuppressWarnings("deprecation")
        public boolean createReflectedImages() {
            // The gap we want between the reflection and the original image
            final int reflectionGap = 4;

            int index = 0;
            for (int imageId : mImageIds) {
                Bitmap originalImage = BitmapFactory.decodeResource(
                        getResources(), imageId);
                int width = originalImage.getWidth();
                int height = originalImage.getHeight();

                // This will not scale but will flip on the Y axis
                Matrix matrix = new Matrix();
                matrix.preScale(1, -1);

                // Create a Bitmap with the flip matrix applied to it.
                // We only want the bottom half of the image
                Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
                        height / 2, width, height / 2, matrix, false);

                // Create a new bitmap with same width but taller to fit
                // reflection
                Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
                        (height + height / 2), Config.ARGB_8888);

                // Create a new Canvas with the bitmap that's big enough for
                // the image plus gap plus reflection
                Canvas canvas = new Canvas(bitmapWithReflection);
                // Draw in the original image
                canvas.drawBitmap(originalImage, 0, 0, null);
                // Draw in the gap
                Paint deafaultPaint = new Paint();
                canvas.drawRect(0, height, width, height + reflectionGap,
                        deafaultPaint);
                // Draw in the reflection
                canvas.drawBitmap(reflectionImage, 0, height + reflectionGap,
                        null);

                // Create a shader that is a linear gradient that covers the
                // reflection
                Paint paint = new Paint();
                LinearGradient shader = new LinearGradient(0,
                        originalImage.getHeight(), 0,
                        bitmapWithReflection.getHeight() + reflectionGap,
                        0x70ffffff, 0x00ffffff, TileMode.CLAMP);
                // Set the paint to use this shader (linear gradient)
                paint.setShader(shader);
                // Set the Transfer mode to be porter duff and destination in
                paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
                // Draw a rectangle using the paint with our linear gradient
                canvas.drawRect(0, height, width,
                        bitmapWithReflection.getHeight() + reflectionGap, paint);

                ImageView imageView = new ImageView(mContext);
                imageView.setImageBitmap(bitmapWithReflection);
                android.widget.Gallery.LayoutParams imgLayout = new CoverFlow.LayoutParams(
                        320, 480);
                imageView.setLayoutParams(imgLayout);
                imageView.setPadding(30, 100, 20, 20);
                mImages[index++] = imageView;

            }
            return true;
        }

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

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

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

        @SuppressLint("NewApi")
        @SuppressWarnings("deprecation")
        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(180, 240));
            i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

            i.setRotation(270);

            // 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)));

        }

    }
    @Override
    public void onClick(View view)
    {
        // TODO Auto-generated method stub
        if(view==btn_back)
        {
            mActivity.pushFragments(com.media.contractcar.base.AppConstants.TAB_HOME, new Home(), true, true);
        }
        if(view==btn_up)
        {
            try
            {
            cover.setSelection(myposition+1,true);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        if(view==btn_down)
        {
            if(myposition>0)
            {
                cover.setSelection(myposition-1,true);
            }

        }
    }

}

0 个答案:

没有答案