1

i am continuously searching and trying to sort out a issue in custom view and Relative layout. i found multiple solution but only few of solution are usefull,but they not fulfilling my requirement completely.

partial solution 1.

partial solution 2.

via these solution i can manage the height and width of my custom view, but i just want to align my custom view in center of parent RelativeLayout, code which i am trying throw hit-and-run is below. ;) JAVA CODE

public class MyActiveView extends View {
public Movie mMovie;
public long movieStart;
private int gifId;



public MyActiveView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeView();
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
    this.setMeasuredDimension(parentWidth / 2, parentHeight);
}


private void initializeView() {
    InputStream is = getContext().getResources().openRawResource(R.raw.pj_logo1);
    mMovie = Movie.decodeStream(is);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);
    long now = android.os.SystemClock.uptimeMillis();
    if (movieStart == 0) {
        movieStart = now;
    }
    if (mMovie != null) {
        int relTime = (int) ((now - movieStart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
        this.invalidate();
    }
}

public int getActiveResource() {
    return this.gifId;
}

public void setActiveResource(int resId) {
    this.gifId = resId;
    initializeView();
}
}

XML CODE

<?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">

<ImageView
    android:id="@+id/backsourceImagesplash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    android:src="@drawable/bg_blur" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@color/colorTransparentWhite" />


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:layout_centerVertical="true"
    android:gravity="centre">

    <pj.com.pjlib.activity_base.support_class.MyActiveView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

i want to set my custom active view layout in centre of its parent Relative Layout, but stuked in it. any help will be appreciated.

i want result like this. but result of my code is

check the alignment of both image,i am trying for attribute android:layout_centerInParent="true", according to Relative layout support the "congrats image" should be in center, but it's not happening.

so i am thinking,i missed something in my custom view class but what is that, i don't know.

Community
  • 1
  • 1

1 Answers1

0

Change android:gravity="centre" with android:gravity="center" in Relative Layout. I think this will helpful to you.

Hemant
  • 23
  • 5