rahstame
rahstame

Reputation: 2178

Image Default Display is not getting Centered

I have this activity that displays image, I am expecting it to be displayed in center by default but what it turns that the image is displayed at the top left of the screen and by the time I press the image that the time if goes to the center.

zoom xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" >

    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/tv_current_matrix"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:gravity="center"
        android:background="#60000000"
        android:textColor="@android:color/white" />

</FrameLayout>

Activity

public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        Window window = getWindow();
        window.setGravity(Gravity.CENTER);

        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));

        Intent intent = getIntent();
        easyPuzzle = intent.getExtras().getString("imagePath");

        setContentView(R.layout.zoom_image);


        mImageView = (ImageView) findViewById(R.id.iv_photo);

        mCurrMatrixTv = (TextView) findViewById(R.id.tv_current_matrix);


        //aq.id(R.id.iv_photo).image(easyPuzzle.toString(), memCache, fileCache);
        Log.d("#Zoom Picture CLass: Image Path ", ""+ easyPuzzle.toString());

        //
        //mImageView.setImageResource(R.drawable.beverages);
        //new DisplayImage(mImageView)
       //.execute(easyPuzzle);


        DisplayImageOptions options = new DisplayImageOptions.Builder()
        //Because you reuse view for different
        //images you can see a previous image in the view while new image is loading. .resetViewBeforeLoading(true
        .showImageForEmptyUri(R.drawable.content_picture)
        .showImageOnFail(R.drawable.content_picture)
        .cacheOnDisc(true)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .build();

        if(imageLoader!=null){
        imageLoader.displayImage(easyPuzzle.toString(), mImageView, options);
        }

        // The MAGIC happens here!
        mAttacher = new PhotoViewAttacher(mImageView);

        // Lets attach some listeners, not required though!
        mAttacher.setOnMatrixChangeListener(new MatrixChangeListener());

        mAttacher.update();
    }

Update

Sorry I forgot to mention that the image is capable of zooming in. SO there is a possibility that the image will be stretched from the center to full screen.

I am using Universal-Image-Loader to display the image, and if I use a drawable the image gets centered right away, but if I switch to Universal-Image-Loader, the image is first displayed at the topmost left of the screen then If I touch it, then transferred to the center.

Upvotes: 0

Views: 518

Answers (3)

Hamad
Hamad

Reputation: 5152

use android:scaletype="center"

<ImageView
    android:id="@+id/iv_photo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" 
    android:scaleType="center"/>

or make height and width wrap_content then it will work

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

this make control in center according to parent view

android:layout_gravity="center"

Upvotes: 0

AnAndroid
AnAndroid

Reputation: 567

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_gravity="center" />

or you can use relative layout as follows..

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher" />

Upvotes: 0

Andrew T.
Andrew T.

Reputation: 4707

Try using android:scaleType instead. (ImageView Scale Type)

<ImageView
    android:id="@+id/iv_photo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="center" />

Upvotes: 1

Related Questions