【问题标题】:Zooming an image of a ScrollView缩放 ScrollView 的图像
【发布时间】:2013-07-14 11:47:45
【问题描述】:

我正在尝试包括放大水平滚动视图。这个想法是用户在他想要突出显示的图像中触摸屏幕,并且该图像会比其他图像大一点。我已经制作了 Horizo​​ntall ScrollView,但我遇到了 OnClick 事件的问题。有什么更好的方法?

这就是我在 MainActivity.java 中的内容

 public class Carrusel extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.iniciar);


    {int[] images = new int[] { R.drawable.image1, R.drawable.image2, 
                                R.drawable.image3, R.drawable.image4,
                                R.drawable.image5 };
    LinearLayout sv = (LinearLayout) findViewById (R.id.carrusel);
    for (int i=0 ; i<5; i++){
       ImageView iv = new ImageView (this);
       iv.setBackgroundResource (images[i]);
       sv.addView(iv);

    }
    }
 }

    @Override   
    public void onClick(View v) {

        switch (v.getId()) {
        case R.drawable.image1:
            //I don´t know what to put here
        break;
        }


    }
    }

这是我在 te main_activity.xml 中的内容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/black" >

 <HorizontalScrollView 
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="10dp"
  >

 <LinearLayout
   android:id="@+id/carrusel"         
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" >
  </LinearLayout>

 </HorizontalScrollView>

   </RelativeLayout>

【问题讨论】:

    标签: android image zooming horizontalscrollview


    【解决方案1】:

    我会这样做:

    MainActivity.java

    public class MainActivity extends Activity implements OnClickListener {
    
        ImageView lastClicked = null;
        int width = 400;
        int height = 320;
        int padding = 30;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LinearLayout l;
            l = (LinearLayout) findViewById(R.id.linear);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                width, height);
            int[] images = new int[] { R.drawable.image1, R.drawable.image2,
                        R.drawable.image3, R.drawable.image4, R.drawable.image5 };
            for (int i = 0; i < 5; i++) {
                ImageView iv = new ImageView(this);
                iv.setLayoutParams(layoutParams);
                iv.setImageResource(images[i]);
                iv.setPadding(padding, padding, padding, padding);
                iv.setOnClickListener(this);
                l.addView(iv);
            }
        }
    
        @Override
        public void onClick(View v) {
            if (v instanceof ImageView) {
                if (lastClicked != null) {
                    lastClicked.setPadding(padding, padding, padding, padding);
                    lastClicked.invalidate();
                }
                v.setPadding(0, 0, 0, 0);
                v.invalidate();
                lastClicked = (ImageView) v;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 2015-01-11
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      相关资源
      最近更新 更多