как добавить обложку (динамическое представление )в main.xml

Я пытался использовать coverflow в своем приложении. я хочу использовать обложку в linearlayout с определенными размерами в моем main.xml.. я пытался, но получаю эту ошибку..

07-27 19:43:23.427: E/AndroidRuntime(1111): FATAL EXCEPTION: main
07-27 19:43:23.427: E/AndroidRuntime(1111): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android/com.android.Coverflow_displayerActivity}: java.lang.NullPointerException
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.os.Looper.loop(Looper.java:132)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.main(ActivityThread.java:4123)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at java.lang.reflect.Method.invokeNative(Native Method)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at java.lang.reflect.Method.invoke(Method.java:491)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at dalvik.system.NativeStart.main(Native Method)
07-27 19:43:23.427: E/AndroidRuntime(1111): Caused by: java.lang.NullPointerException
07-27 19:43:23.427: E/AndroidRuntime(1111):     at com.android.Coverflow_displayerActivity.onCreate(Coverflow_displayerActivity.java:48)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.Activity.performCreate(Activity.java:4397)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
07-27 19:43:23.427: E/AndroidRuntime(1111):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
07-27 19:43:23.427: E/AndroidRuntime(1111):    ... 11 more

вот мой код, который я пробовал

package com.android;

import java.io.FileInputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

import com.android.CoverAdapterView.OnItemClickListener;

public class Coverflow_displayerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        CoverFlow coverFlow = (CoverFlow) findViewById(R.id.coverflow);

//      
//      CoverFlow coverFlow;
//      coverFlow= new CoverFlow(this);

        coverFlow.setAdapter(new ImageAdapter(this));

        ImageAdapter coverImageAdapter = new ImageAdapter(this);

        coverImageAdapter.createReflectedImages();

        coverFlow.setAdapter(coverImageAdapter);

        coverFlow.setSpacing(-15);
        coverFlow.setSelection(5, true);

        setContentView(R.layout.main);
//      setContentView(coverFlow);
        coverFlow.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(CoverAdapterView parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Log.v("this happens only in india", "ok i know this position"
                        + position);
                Toast.makeText(getApplicationContext(), "position " + position,
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Use this if you want to use XML layout file
    //  setContentView(R.layout.main);


    }

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

        private FileInputStream fis;

        private Integer[] mImageIds = { R.drawable.kasabian_kasabian,

        R.drawable.killers_day_and_age, R.drawable.garbage_bleed_like_me,
                R.drawable.death_cub_for_cutie_the_photo_album,
                R.drawable.kasabian_kasabian,
                R.drawable.massive_attack_collected, };

        private ImageView[] mImages;

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

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

            int index = 0;
            for (final 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);

                imageView.setLayoutParams(new CoverFlow.LayoutParams(120, 180));
                imageView.setScaleType(ScaleType.MATRIX);
                mImages[index++] = imageView;

            }
            return true;
        }

        public int getCount() {
            // Log.v("getCoutn(int count)", "" + mImageIds.length);
            return mImageIds.length;
        }

        public Object getItem(int position) {
            // Log.v("getItem(int position)", "" + position);
            return position;
        }

        public long getItemId(int position) {
            // Log.v("getItemID(int position)", "" + position);
            return position;
        }

        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.MATRIX);
            // return i;

            // Log.v("getview int position mImages", ""+position);

            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) */
            Log.v("getScale float ", " " + focused + " " + offset);
            return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
        }

        // View v=getView(position, convertView, parent);

    }

}

main.xml




    

    

     
    


0
задан ashish.n 27 July 2012 в 07:30
поделиться