1

嗨,我正在 Android 中开发应用程序。我正在使用 Coverflow 视图来显示图像。但问题是它显示在屏幕中央。我需要显示在屏幕顶部。请帮助我,我是新手安卓。

代码:我的活动课

 public class NewspaperCoverFlowActivity extends Activity 
{
/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    Coverflow coverFlow;
    coverFlow = new Coverflow(this);

    coverFlow.setAdapter(new ImageAdapter(this));

    ImageAdapter coverImageAdapter =  new ImageAdapter(this);

    coverFlow.setAdapter(coverImageAdapter);

    coverFlow.setSpacing(2);
    coverFlow.setSelection(1, true);
    coverFlow.setAnimationDuration(1500);      
    setContentView(coverFlow);
}



}

Adapter Class:


public class ImageAdapter extends BaseAdapter 
{
    int mGalleryItemBackground;
    private Context mContext;
    static final String URL="XXXXXXXXXXX";
    String[] imageurl ;
    private FileInputStream fis;


    private Integer[] mImageIds = 
     {
      R.drawable.ic_launcher,
      R.drawable.ic_launcher,
      R.drawable.ic_launcher, R.drawable.ic_launcher,  R.drawable.ic_launcher
      };

    private ImageView[] mImages;

    public ImageAdapter(Context c) 
    {                                                                                                                                                                                                                
//      System.out.println("Inside image cons");
     mContext = c;
     getelement();
     createReflectedImages();
//     mImages = new ImageView[imageurl.length];       
    }
    public void getelement()
    {
//      System.out.println("Inside getelement");
//     String[] itemsarray={};
//     ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
//     ArrayList<String> menuItems = new ArrayList<String>();
     TaplistingParser parser = new TaplistingParser();
     String xml= parser.getXmlFromUrl(URL);
     Document doc=parser.getDomElement(xml);        
//     System.out.println("sssss="+doc);
     NodeList nl=doc.getElementsByTagName("article");
     imageurl = new String[nl.getLength()];
     mImages = new ImageView[imageurl.length];

//     System.out.println("len="+nl.getLength());
     for(int i=0; i < nl.getLength(); i++ )
     {
//         System.out.println("Inside for");
//      HashMap<String, String> map = new HashMap<String, String>(); 
        Element e = (Element) nl.item(i);
//      map.put("SectionName", parser.getValue(e, "sectionname"));
//      System.out.println("b4 items="+parser.getValue(e, "sectionname"));
//      itemsarray[i] = parser.getValue(e, "sectionname");
//      System.out.println("items="+itemsarray[i]);
        imageurl[i]=parser.getValue(e, "kickerimage");
//      System.out.println("i am here..."+imageurl[i]);
//      menuItems.add(parser.getValue(e, "sectionname"));
//      menuItems.add(parser.getValue(e, "sectionname"));
//     System.out.println("menu="+menuItems);

     }    
//    String[] itemsarray = menuItems.toArray(new String[menuItems.size()]); 

//     String[] itemsarray = new String[menuItems.size()];
//     itemsarray=menuItems.toArray(itemsarray);
////     for(int j= 0;j < itemsarray.length;j++ )
////     {
////       Log.d("string is",(itemsarray[j]));
////     }
//      return itemsarray;
    }
 public boolean createReflectedImages() {
         //The gap we want between the reflection and the original image
         final int reflectionGap = 4;
         int index = 0;
//         for (int imageId : imageurl)
         for(int i=0;i<imageurl.length;i++)
         {
//           System.out.println("image="+imageurl[i]);
//       Bitmap originalImage = BitmapFactory.decodeResource(getResources(), 
//             imageurl[i]);

             Bitmap originalImage =  DownloadImage(imageurl[i]);

          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;
         }
@Override
public int getCount() 
{
    // TODO Auto-generated method stub
    return imageurl.length;
}
@Override
public Object getItem(int position) 
{
    // TODO Auto-generated method stub
    return imageurl[position];
}
@Override
public long getItemId(int position)
{
    // TODO Auto-generated method stub
    return position;
}
 public View getView(int position, View convertView, ViewGroup parent) 
 {
     //Use this code if you want to load from resources
//      DownloadImage(imageurl[position]);
        Bitmap bit = DownloadImage(imageurl[position]);
        ImageView i = new ImageView(mContext);
        i.setImageBitmap(bit);
//        i.setImageResource(imageurl[position]);
        i.setLayoutParams(new Coverflow.LayoutParams(170, 170));
        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))); 
     }





     Bitmap bitmap = null;
     private Bitmap DownloadImage(String URL)
        {        
//          System.out.println("image inside="+URL);

            InputStream in = null;        
            try
            {
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in);
                in.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
//          System.out.println("image last");
            return bitmap;                
        }
        private InputStream OpenHttpConnection(String urlString)
                throws IOException
                {
                    InputStream in = null;
                    int response = -1;

                    URL url = new URL(urlString);
                    URLConnection conn = url.openConnection();

                    if (!(conn instanceof HttpURLConnection))                    
                        throw new IOException("Not an HTTP connection");                   
                    try{
                        HttpURLConnection httpConn = (HttpURLConnection) conn;
                        httpConn.setAllowUserInteraction(false);
                        httpConn.setInstanceFollowRedirects(true);
                        httpConn.setRequestMethod("GET");
                        httpConn.connect();

                        response = httpConn.getResponseCode();                
                        if (response == HttpURLConnection.HTTP_OK) 
                        {
                            in = httpConn.getInputStream();                                
                        }                    
                    }
                    catch (Exception ex)
                    {
                        throw new IOException("Error connecting");            
                    }
                    return in;    
        }
        }

覆盖流类:

  public class Coverflow extends Gallery
    {

         private Camera mCamera = new Camera();
         private int mMaxRotationAngle = 60;
         private int mMaxZoom = -120;
         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);   
              }

              public int getMaxRotationAngle() {
                  return mMaxRotationAngle;
                 }
              public void setMaxRotationAngle(int maxRotationAngle) {
                  mMaxRotationAngle = maxRotationAngle;
                 }

              public int getMaxZoom() {
                  return mMaxZoom;
                 }
              public void setMaxZoom(int maxZoom) {
                  mMaxZoom = maxZoom;
                 }

              private int getCenterOfCoverflow() {
                    return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
                }
              private static int getCenterOfView(View view) {
                    return view.getLeft() + view.getWidth() / 2;
                }  
              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;
                 }

              protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                  mCoveflowCenter = getCenterOfCoverflow();
                  super.onSizeChanged(w, h, oldw, oldh);
                 }

              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 = (float) (mMaxZoom +  (rotation * 1.5));
                   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();
             }
            }
4

1 回答 1

0

在您的 OnCreate
添加以下行

        coverFlow.setGravity(Gravity.TOP);

这会将您的coverflow的重力设置为TOP

于 2012-12-11T08:44:41.767 回答