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

これも私の問題でした。長い間、私は自分自身を試してきました。それは楽しいです。ImageView を View に使用/キャストする場所を変更するだけです。したがって、 ImageAdapter の getView() では、必要に応じてビューを返すことができます:)。私の英語でごめんなさい。

于 2012-06-01T10:05:28.477 に答える