简体   繁体   English

在Android中使用TextView进行Coverflow

[英]Coverflow with textview in android

Hello I am creating an app in which i am using CoverFlow. 您好,我正在创建一个使用CoverFlow的应用程序。 But i want to add textview with imageview. 但是我想用textview添加textview。 I have changed AbstractCoverFlowImageAdapter like below. 我已经如下更改了AbstractCoverFlowImageAdapter。

/**
 * This class is an adapter that provides base, abstract class for images
 * adapter.
 * 
 */
public abstract class AbstractCoverFlowImageAdapter extends BaseAdapter {

    /** The Constant TAG. */
    private static final String TAG = AbstractCoverFlowImageAdapter.class.getSimpleName();

    /** The width. */
    private float width = 0;

    /** The height. */
    private float height = 0;

    /** The bitmap map. */
    private final Map<Integer, WeakReference<Bitmap>> bitmapMap = new HashMap<Integer, WeakReference<Bitmap>>();

    public AbstractCoverFlowImageAdapter() {
        super();
    }

    /**
     * Set width for all pictures.
     * 
     * @param width
     *            picture height
     */
    public synchronized void setWidth(final float width) {
        this.width = width;
    }

    /**
     * Set height for all pictures.
     * 
     * @param height
     *            picture height
     */
    public synchronized void setHeight(final float height) {
        this.height = height;
    }

    @Override
    public final Bitmap getItem(final int position) {
        final WeakReference<Bitmap> weakBitmapReference = bitmapMap.get(position);
        if (weakBitmapReference != null) {
            final Bitmap bitmap = weakBitmapReference.get();
            if (bitmap == null) {
                Log.v(TAG, "Empty bitmap reference at position: " + position + ":" + this);
            } else {
                Log.v(TAG, "Reusing bitmap item at position: " + position + ":" + this);
                return bitmap;
            }
        }
        Log.v(TAG, "Creating item at position: " + position + ":" + this);
        final Bitmap bitmap = createBitmap(position);
        bitmapMap.put(position, new WeakReference<Bitmap>(bitmap));
        Log.v(TAG, "Created item at position: " + position + ":" + this);
        return bitmap;
    }

    /**
     * Creates new bitmap for the position specified.
     * 
     * @param position
     *            position
     * @return Bitmap created
     */
    protected abstract Bitmap createBitmap(int position);

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public final synchronized long getItemId(final int position) {
        return position;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getView(int, android.view.View,
     * android.view.ViewGroup)
     */


    public final synchronized View getView(final int position, final View convertView, final ViewGroup parent) {
      //  ImageView imageView;
        View root;
        if (convertView == null) {
             final Context context = parent.getContext();
             LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            root = mInflater.inflate(R.layout.coverflowitem, null);
            root.setLayoutParams(new CoverFlow.LayoutParams(150, 150));
        } else {
            Log.v(TAG, "Reusing view at position: " + position + ":" + this);
            root = convertView;
        }
        ImageView imageView = (ImageView)root.findViewById(R.id.coverflow_image);
        TextView text = (TextView)root.findViewById(R.id.descriptionText);
        imageView.setImageBitmap(getItem(position));
        text.setText("You Win!");

        return root;
    }

}

I have changed the getView() method's return type to View from ImageView. 我已经将getView()方法的返回类型更改为ImageView的View。

And my coverflow xml and coverflow items xml are blow respectively 我的Coverflow xml和Coverflow项目xml分别被打击

coverflow.xml coverflow.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/women3">

    <com.android.jigsawpuzzle.puzzle.coverflow.CoverFlow
        xmlns:coverflow="http://schemas.android.com/apk/res-auto/org.worldsproject.puzzle"
        android:id="@+id/coverflow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        coverflow:imageHeight="250dip"
        coverflow:imageReflectionRatio="0.2"
        coverflow:imageWidth="250dip"
        coverflow:reflectionGap="2dip"
        coverflow:withReflection="true" />

    <Button
        android:id="@+id/difficulty"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:text="@string/easy" 
        android:layout_marginBottom="10dp"
        android:background="@drawable/profile_border"/>

</RelativeLayout>

coverflowitems.xml coverflowitems.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView android:id="@+id/coverflow_image"
               android:layout_width="100dp"
               android:layout_height="100dp"
               android:layout_gravity="center"
               android:contentDescription="image"/>

    <ScrollView android:id="@+id/sv"
                android:layout_width="250dp"
                android:layout_height="150dp"
                android:background="@drawable/profile_border"
                android:layout_gravity="center_horizontal">

        <TextView android:id="@+id/descriptionText"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="left"
                  android:textColor="@color/black"
                  android:text="Image description"/>

    </ScrollView>


</LinearLayout>

Now my problem is that i am getting an error while running the app:- 现在我的问题是运行该应用程序时出现错误:-

07-14 12:43:03.089: E/AndroidRuntime(9976): FATAL EXCEPTION: main
07-14 12:43:03.089: E/AndroidRuntime(9976): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ImageView
07-14 12:43:03.089: E/AndroidRuntime(9976):     at CoverFlow.getChildStaticTransformation(CoverFlow.java:288)

Please help to get rid of this error and redirects me to the working code. 请帮助摆脱该错误,并将我重定向到工作代码。

Thanks in advance. 提前致谢。

It looks like coverflow implementation you are using is made only for ImageView and not for general views. 看起来您正在使用的Coverflow实现仅用于ImageView,而不用于常规视图。

Take a look here for example https://github.com/applm/ma-components 看看这里的例子https://github.com/applm/ma-components

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM