使用ImageView的TextView选框

时间:2012-10-09 10:51:33

标签: android textview imageview marquee

我需要在图像中显示textview和imageview的选框。 where the textview along with the imageview will be moving 如果单击textview或Imageview,它应该打开一个活动。有人可以建议一种方法。

ImageView应该是可点击的。

3 个答案:

答案 0 :(得分:3)

我在Horizo​​ntalView中的图像视图上实现了选框,类似于您在问题中描述的,使用CountDownTimer

对于您的问题,您必须在Horizo​​ntalView中的一个布局中使用texviewImagview并在Horizo​​ntalView上应用选框,

查看代码#

    public class MyCounter extends CountDownTimer {

    public MyCounter(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        if (leftToRight == true) {

            MCObject.cancel();
            //if (gallery.getScrollX() > (((imgv.length - 1) * multiplyValue)+addValue)) {

            if (gallery.getScrollX() > ((imgv.length - 1) * multiplyValue)+addValue) {
                count=((imgv.length - 1) *imageHeightWidth);
                leftToRight = false;
                MCObject.start();
            }

            if (count != gallery.getScrollX()) {
                gallery.scrollBy(1, 0);

                count++;
                MCObject.start();
            } else {
                count=((imgv.length - 1) * imageHeightWidth);
                leftToRight = false;
                MCObject.start();
            }
        } else {
            MCObject.cancel();
            if (gallery.getScrollX() <= 0) {
                count = -20;
                leftToRight = true;
                MCObject = new MyCounter(50, 1);
                MCObject.start();
            }

            if (count != 0) {
                gallery.scrollBy(-1, 0);

                // Log.d("test", ""+hsv.getScrollX());
                count--;
                MCObject.start();
            } else {

                count = -20;
                leftToRight = true;
                MCObject = new MyCounter(50, 1);
                MCObject.start();

            }
        }
    }

    @Override
    public void onTick(long millisUntilFinished) {

    }

}

它会从左到右直到长度,然后从右到左反转,两个边框。

在运行时,我在布局中添加图像,其中在水平视图内的xml中声明了id, 选中此功能可在运行时添加图像。

public void LLImageView() {
    imgv = new ImageView[25];
    for (int j = 0; j < 25; j++) {
        imgv[j] = new ImageView(this);

        img = new ImageView(MainScreenAnim.this);
        para = new LinearLayout.LayoutParams(imageHeightWidth,imageHeightWidth);

        para.leftMargin = 10;
        para.topMargin = 5;
        imgv[j].setOnClickListener(MainScreenAnim.this);
        imgv[j].setBackgroundResource(Imgid[j]);

        layoutHorizontal.addView(imgv[j], para);

        images.add(Imgid[j].toString());

        System.out.println("string arraylist@@@@@@@" + images.get(j));
    }

}

Inside OnCreate#

MCObject = new MyCounter(50, 1);
    MCObject.start();

我要显示固定数量的图像。对于处理点击事件,请查看以下代码段。

    public void onClick(View v) {
    for (int j = 0; j < 25; j++) {
        if (v == imgv[j]) {

            //do something
        }
    }

}

答案 1 :(得分:1)

创建自定义视图

public class MarqueeLayout extends ViewGroup {

private static final int VERTICAL_SPACING = 10;
private static final int HORIZONTAL_SPACING = 10;
private static final String TAG = MarqueeLayout.class.getName();
private int line_width;
private List<View> views; 
private Timer timer;

private int scrollX = 0;

public MarqueeLayout(Context context)
{
    super(context);
}
private Handler handler;
private int index = 0;
private int childCount;
public MarqueeLayout(Context context, AttributeSet attrs)
{
    super(context, attrs);
    handler = new Handler();
    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    requestLayout();
                }
            });
        }
    }, 1000, 200);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);
    if(views == null) {
        views = new ArrayList<View>();
        childCount = getChildCount();
        for(int i = 0; i < childCount; i++) {
            views.add(getChildAt(i));
        }

    }
    final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
    int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
    final int count = getChildCount();
    int line_height = 0;

    int xpos = getPaddingLeft();
    int ypos = getPaddingTop();

    int childHeightMeasureSpec;
    if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST)
    {
        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
    }
    else
    {
        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }

    for (int i = 0; i < count; i++)
    {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE)
        {
            child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);
            final int childw = child.getMeasuredWidth();
            line_height = Math.max(line_height, child.getMeasuredHeight() + VERTICAL_SPACING);

            xpos += childw + HORIZONTAL_SPACING;
        }
    }
    this.line_width = xpos;

    setMeasuredDimension(width, height);
}

@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams()
{
    return new LayoutParams(1, 1); 
}

@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p)
{
    if (p instanceof LayoutParams)
    {
        return true;
    }
    return false;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
    Log.d(TAG, "onLayout called");
    int count = getChildCount();
    final int width = r - l;

    scrollX -= 20;
    if(line_width + scrollX < 0) {
        scrollX = 0;
    }
    int i = 0;
    while(count > 0) {
        View c = getChildAt(i);
        if(c == null) {
            break;
        }
        int w = c.getMeasuredWidth();
        Log.d(TAG, "scrollX : " + scrollX + " width : " + w);
        if(scrollX < -w) {
            this.removeViewAt(0);
            scrollX += w;
        } else {
            break;
        }
        i++;
        count--;
    }
    count = getChildCount();
    int xpos = getPaddingLeft() + scrollX;
    int ypos = getPaddingTop();
    for (i = 0; i < count; i++)
    {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE)
        {
            final int childw = child.getMeasuredWidth();
            final int childh = child.getMeasuredHeight();
            child.layout(xpos, ypos, xpos + childw, ypos + childh);
            xpos += childw + HORIZONTAL_SPACING;
        }
    }
    while(xpos < getWidth()) {
        View v = views.get(index % childCount);
        addView(v);
        xpos += v.getMeasuredWidth();
        index++;
    }
}} 

并且您可以在此布局中包含任何类型的视图,您可以在xml中调用此布局

<Your.Package.name.MarqueeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is textview 1"
        tools:context=".MainActivity" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clicked"
        android:text="Click me 1" />

</Your.Package.name.MarqueeLayout>

答案 2 :(得分:1)

  • 我使用TextView和android:ellipsize =“marquee”。它运作良好,但其中一个缺点是当品牌运行继续,然后堆大小增加10到20分钟应用程序崩溃。所以我使用了RecycleView选项。它的工作非常好,不会增加堆大小。

  • 可以使用recycleView在marque中设置文本和图像。我终于浪费了两天,我找到了正确的解决方案recycleView。请按照此步骤进行操作。

步骤1:在片段或活动布局中使用recycleView

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/marqueList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:clipToPadding="false" />

 </LinearLayout>

步骤2:为recycleView创建适配器布局,名称为marque_adapter.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:layout_marginTop="2dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/arrow_up"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="One"
        />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/arrow_down"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="Two"
       />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/arrow_up"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="Three"
        />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/arrow_down"/>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="Four"
     />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

</LinearLayout>

Step3:为RecycView创建适配器,名称为MarqueAdapter.java

public class MarqueAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public class ViewHolderItem extends RecyclerView.ViewHolder {
    public ViewHolderItem(View view) {
        super(view);
 }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.marque_adapter, parent, false);
    viewHolder = new ViewHolderItem(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}

@Override
public int getItemCount() {
    // You can make count infinite time
    return 1000;
}}

步骤4:最后在您的片段或活动中执行此操作,其中recycleView定义

  private final Handler mHandler = new Handler(Looper.getMainLooper());
  private final Runnable SCROLLING_RUNNABLE = new Runnable() {
    @Override
    public void run() {
        final int duration = 20;
        final int pixelsToMove = 20;
        mRecycleMarqueList.smoothScrollBy(pixelsToMove, 0);
        mHandler.postDelayed(this, duration);
    }
};

RecyclerView mRecycleMarqueList = (RecyclerView) view.findViewById(marqueList);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecycleMarqueList.setLayoutManager(layoutManager);
    MarqueAdapter mMarqueAdapter = new MarqueAdapter();
    mRecycleMarqueList.setAdapter(mMarqueAdapter);
    mHandler.post(SCROLLING_RUNNABLE);

最后看起来像屏幕下方

enter image description here

enter image description here