1

我正在尝试使用布局进行覆盖流,图像我没有问题并且工作完美,问题是尝试使用 los*.xml 来完成。

我有以下错误:

 05-14 09:14:56.901: WARN/ImageView(774): Unable to find resource: 2130903041
    05-14 09:14:56.901: WARN/ImageView(774): android.content.res.Resources$NotFoundException: File res/layout/prueba1.xml from drawable resource ID #0x7f030001
    05-14 09:14:56.901: WARN/ImageView(774):     at android.content.res.Resources.loadDrawable(Resources.java:1697)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.content.res.Resources.getDrawable(Resources.java:581)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.widget.ImageView.resolveUri(ImageView.java:489)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.widget.ImageView.setImageResource(ImageView.java:274)
    05-14 09:14:56.901: WARN/ImageView(774):     at com.example.coverflow.CoverFlowExample$ImageAdapter.getView(CoverFlowExample.java:77)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.widget.Gallery.makeAndAddView(Gallery.java:745)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.widget.Gallery.layout(Gallery.java:622)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.widget.AbsSpinner.setSelectionInt(AbsSpinner.java:292)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.widget.AbsSpinner.setSelection(AbsSpinner.java:269)
    05-14 09:14:56.901: WARN/ImageView(774):     at com.example.coverflow.CoverFlowExample.onCreate(CoverFlowExample.java:30)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.os.Looper.loop(Looper.java:123)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    05-14 09:14:56.901: WARN/ImageView(774):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-14 09:14:56.901: WARN/ImageView(774):     at java.lang.reflect.Method.invoke(Method.java:521)
    05-14 09:14:56.901: WARN/ImageView(774):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    05-14 09:14:56.901: WARN/ImageView(774):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    05-14 09:14:56.901: WARN/ImageView(774):     at dalvik.system.NativeStart.main(Native Method)
    05-14 09:14:56.901: WARN/ImageView(774): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag LinearLayout
    05-14 09:14:56.901: WARN/ImageView(774):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:784)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
    05-14 09:14:56.901: WARN/ImageView(774):     at android.content.res.Resources.loadDrawable(Resources.java:1694)

这是代码:

    package com.example.coverflow;

import java.io.FileInputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class CoverFlowExample 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(-25);
     coverFlow.setSelection(0, true);
     coverFlow.setAnimationDuration(1000);


     setContentView(coverFlow);
    }

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

     private FileInputStream fis;

//     private Integer[] mImageIds = {
////             R.drawable.twitter,
////             R.drawable.centros,
////             R.drawable.seas,
////             R.drawable.estudios,
////             R.drawable.videos
//     };

     private ImageView[] mImages;

     Context context;
        LayoutInflater v1 = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View p1 = v1.inflate(R.layout.prueba1, null);

        LayoutInflater v2 = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View p2 = v2.inflate(R.layout.prueba2, null);


        LayoutInflater v3 = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View p3 = v3.inflate(R.layout.prueba3, null);


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

     public int getCount() {
         return 1;
     }

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

     public long getItemId(int 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);
         Bitmap bmp[] = new Bitmap[3];
         bmp[0]=p1.getDrawingCache(true);
         bmp[1]=p2.getDrawingCache(true);
         bmp[2]=p3.getDrawingCache(true);
//         i.setImageDrawable(drawable[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))); 
      } 

 }
}

它应该只用视图替换图像数组吗?谢谢

4

2 回答 2

2

据我了解你的问题,问题是,

您正在尝试将 XML 布局文件设置为 Imageviews 的可绘制对象。

要使其工作,请从您的 XML 布局文件创建视图,然后从中制作位图并将这些位图设置为您的 ImageView。然后它工作..

代码:

public class ImageAdapter extends BaseAdapter {
     int mGalleryItemBackground;
     private Context mContext;
     private FileInputStream fis;
     private ImageView[] mImages;
     Bitmap bmp[] = new Bitmap[3];
     Context context;

     public ImageAdapter(Context c) {
      mContext = c;
      LayoutInflater v1 = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View p1 = v1.inflate(R.layout.prueba1, null);
      p1.setDrawingCacheEnabled(true);
      // this is the important code :)  
      // Without it the view will have a dimension of 0,0 and the bitmap will be null          
      p1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
      p1.layout(0, 0, p1.getMeasuredWidth(), p1.getMeasuredHeight()); 

      p1.buildDrawingCache(true);
      bmp[0] = Bitmap.createBitmap(p1.getDrawingCache());
      p1.setDrawingCacheEnabled(false); // clear drawing cache

      View p2 = v2.inflate(R.layout.prueba2, null);
      p2.setDrawingCacheEnabled(true);
      // this is the important code :)  
      // Without it the view will have a dimension of 0,0 and the bitmap will be null          
      p2.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
      p2.layout(0, 0, p2.getMeasuredWidth(), p2.getMeasuredHeight()); 

      p2.buildDrawingCache(true);
      bmp[1] = Bitmap.createBitmap(p2.getDrawingCache());
      p2.setDrawingCacheEnabled(false); // clear drawing cache     

      View p3 = v3.inflate(R.layout.prueba3, null);
      p3.setDrawingCacheEnabled(true);
      // this is the important code :)  
      // Without it the view will have a dimension of 0,0 and the bitmap will be null          
      p3.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
      p3.layout(0, 0, p3.getMeasuredWidth(), p3.getMeasuredHeight()); 

      p3.buildDrawingCache(true);
      bmp[2] = Bitmap.createBitmap(p3.getDrawingCache());
      p3.setDrawingCacheEnabled(false); // clear drawing cache   
     }

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

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

     public long getItemId(int 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.setImageBitmap(bmp[position]); 
         i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
         i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

        return i;
     }
   /** 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))); 
      } 
 }
于 2012-05-14T09:52:16.303 回答
0

这也是我的问题。很长一段时间我自己都试过了。好好玩。我在 CoverFlow.java 中找到,只需更改使用/将 ImageView 转换为 View 的地方。因此,在 ImageAdapter 的 getView() 中,您可以根据需要返回视图 :)。对不起我的英语不好。

于 2012-06-01T10:05:28.477 回答