yuva ツ
yuva ツ

Reputation: 3703

adding both horizontal and vertical scrollview for imageview

in my application.i have used RelativeLayout. i want to add horizontal and vertical scrollview to imageview. i tried it but not working. imageview size should be 800*800. is there need of LinearLayout. how can i do this?please help me.

xml file -

  <?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:background="@color/Beige"
android:focusable="true"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linearLayout1"
    style="@color/Bisque"
    android:layout_width="250dp"
    android:layout_height="fill_parent"
    android:background="@color/Plum"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:background="@color/Thistle"
        android:onClick="importFile"
        android:text="@string/Import" />

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="50dp"
        android:background="@color/Thistle"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="210dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="50dp"
        android:background="@color/Thistle"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="180dp"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/yes"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_weight="0.15"
                android:background="@color/Thistle"
                android:focusable="false"
                android:text="@string/X"
                android:textColor="@color/PaleVioletRed"
                android:textColorHint="@color/PaleVioletRed"
                android:textSize="35sp"
                android:textStyle="bold"
                android:typeface="normal"
                android:width="4dp" />

            <Button
                android:id="@+id/no"
                android:layout_width="30dp"
                android:layout_height="40dp"
                android:layout_marginLeft="60dp"
                android:background="@drawable/cor" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="180dp"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/back"
                android:layout_width="40dp"
                android:layout_height="30dp"
                android:layout_marginTop="5dp"
                android:background="@color/Thistle"
                android:text="@string/l"
                android:textColor="@color/PaleVioletRed"
                android:textColorHint="@color/PaleVioletRed"
                android:textSize="30sp"
                android:textStyle="bold"
                android:typeface="sans" />

            <TextView
                android:id="@+id/color"
                android:layout_width="40dp"
                android:layout_height="30dp"
                android:layout_marginLeft="10dp"
                android:background="@color/Black" />

            <Button
                android:id="@+id/next"
                android:layout_width="40dp"
                android:layout_height="30dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="6dp"
                android:background="@color/Thistle"
                android:text="@string/r"
                android:textColor="@color/PaleVioletRed"
                android:textSize="30sp"
                android:textStyle="bold"
                android:typeface="sans" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="180dp"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/sub"
                android:layout_width="40dp"
                android:layout_height="30dp"
                android:background="@drawable/plus"
                android:textSize="40sp" />

            <Button
                android:id="@+id/add"
                android:layout_width="40dp"
                android:layout_height="30dp"
                android:layout_marginLeft="60dp"
                android:background="@drawable/minus" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/linearLayout1"
    android:background="@color/Thistle"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/save"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        android:background="@color/MistyRose"
        android:text="@string/Save" />

    <Button
        android:id="@+id/send"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="30dp"
        android:background="@color/MistyRose"
        android:text="@string/Send" />
</LinearLayout>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="800dp"
    android:layout_height="800dp"

    android:layout_alignLeft="@+id/linearLayout2"
    android:layout_below="@+id/linearLayout2" >

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:orientation="vertical"
        >


       <HorizontalScrollView 
       android:id="@+id/hv"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
       >
             <ImageView 
           android:id="@+id/imv"
           android:background="@drawable/blu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

           />
   </HorizontalScrollView>
      </LinearLayout>
</ScrollView>

Upvotes: 0

Views: 2806

Answers (1)

Siddharth Lele
Siddharth Lele

Reputation: 27748

Instead of trying to fit an Image in a ScrollView and a HorizontalScrollView to enable image panning, which I am not even sure can be implemented, and if it can, could prove to be quite a herculean task.

You might consider using the PhotoView library. It is incredibly simple to implement and works like a charm out-of-the-box. Plus, as a bonus, it supports zooming, using multi-touch and double-tap.

On that page is a simple example of how to implement it. All it needs is an ImageView setup in your XML. No ScrollViews or HorizontalScrollViews needed to get it to work:

ImageView mImageView;
PhotoViewAttacher mAttacher;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Any implementation of ImageView can be used!
    mImageView = (ImageView) findViewById(R.id.iv_photo);

    // Set the Drawable displayed
    Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
    mImageView.setImageDrawable(bitmap);

    // Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
    mAttacher = new PhotoViewAttacher(mImageView);
}


// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call
attacher.update();

Upvotes: 3

Related Questions