Android 包含视图的Coverflow
我正在尝试用布局做coverflow,用图像我没有问题,而且效果很好,问题是试着用los*.xml来做 我有以下错误:Android 包含视图的Coverflow,android,widget,gallery,coverflow,Android,Widget,Gallery,Coverflow,我正在尝试用布局做coverflow,用图像我没有问题,而且效果很好,问题是试着用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/pr
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)));
}
}
}
仅用视图替换图像数组应该有效吗?
谢谢我理解你的问题,问题是 您正在尝试将XML布局文件设置为可绘制到ImageView的文件 要使其工作,请从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)));
}
}
这也是我的问题。很长时间以来,我一直在尝试自己。那很有趣。我在CoverFlow.java中发现,只需将使用/转换ImageView的地方更改为View。因此,在ImageAdapter的getView()中,您可以根据需要返回视图:)。对不起,我的英语不好。从您的错误中,似乎缺少了一些东西,您是否尝试过清理&构建(Project->clean…)?我的xml布局很简单,只想在coverflow上看到,3个测试文件之一是以下代码:我很好地遵循了您的步骤?语境;LayoutInflater v1=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);视图p1=v1.充气(R.layout.PrueB1,空);位图bMp1=loadBitmapFromView(p1);LayoutInflater v2=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);视图p2=v2.充气(R.layout.prueb2,空);位图bMp2=loadBitmapFromView(p2);LayoutInflater v3=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);视图p3=v3.充气(R.layout.prueba3,空);位图bMp3=loadBitmapFromView(p3);公共位图loadBitmapFromView(视图v){Bitmap b=Bitmap.createBitmap(v.getLayoutParams().width,v.getLayoutParams().height,Bitmap.Config.ARGB_8888);Canvas c=新画布(b);v.layout(0,0,v.getLayoutParams().width,v.getLayoutParams().height);v.draw(c);返回b;}如何将此位图设置为ImageView??感谢删除此行i.setImageResource(mimageId[position]);谢谢,但是现在空指针异常=(我想在这一行,位图b=Bitmap.createBitmap(v.getLayoutParams().width,v.getLayoutParams().height,Bitmap.Config.ARGB_8888);好的,添加强制转换到这个?drawable[0]=((ImageView)p1.getDrawable();至此,java.lang.ClassCastException:android.widget.LinearLayout位于最后一行-1000感谢您的回复-