java - 安卓用户界面 : Positioning of custom GIF View within RelativeLayout

标签 java android

因此,我创建了自己的自定义 GIFView 组件来显示动画 GIF。我的问题是 Android 无法将其正确放置在中心位置。我试图从各种来源寻找这个,但大多数都是关于如何动态地执行此操作的,而我想纯粹使用 XML 布局来执行此操作。

问题是这样的:

  1. 如果我不覆盖“onMeasure()”,那么我的 GIFView 似乎会占用屏幕中的所有空间。这导致布局中的所有元素都被绘制到屏幕的最顶部。我的 GIF 就像一个进度条(高度 = 8px,宽度 = 152px),所以它不需要太多高度。无论如何,Android 是这么认为的,并给它整个屏幕(我的解释基于给 onMeasure 方法的 MeasureSpecs)。动画现在就在 TextView 下方,但不在中心区域。相反,它位于该区域的左上角。

  2. 如果我确实覆盖了“onMeasure()”,那么 TextView 会在屏幕中间正确绘制,进度动画会在其下方绘制。但是,在这种情况下,动画 GIF 的位置也位于该区域的左上角,而它应该位于该区域的中间。

  3. 如果我确实覆盖了“onMeasure()”,然后根据需要调用“setMeasuredDimension()”来设置测量值,但不调用 super.onMeasure(),那么它的行为就像以防万一 ( 1).这意味着布局占据了整个屏幕,我的 TextView 可以从屏幕的最顶部找到。

我不确定我是否有任何意义,所以我尝试在这里从数学意义上给你这个想法(我无法发布屏幕截图,因为我是新用户)。因此,相对布局在 TextView 正下方提供与屏幕一样宽 (screenWidth) 和所需高度 (paddingTop + paddingBottom + GIFView.getHeight()) 的区域。现在我希望看到我的动画从以下位置开始绘制:(x = paddingLeft + (screenWidth - paddingLeft - paddingRight - GIFView.getWidth())/2) 和 (y = paddingTop)。相反,我看到 Android 将其绘制到 (x = 0) 和 (y = 0) 的位置。

我确信这里的问题是我刚刚忽略的,但如果有人有时间研究这个,我将不胜感激......

所以,我遇到这个问题的 fragment :

样式声明:

<declare-styleable name="GIFView">
    <attr name="resourceId" format="reference" />
</declare-styleable>

主要布局 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:paddingLeft="10dip"
   android:paddingRight="10dip"
   android:isScrollContainer="true"
   android:background="#EFEFEF" >  

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:custom="http://schemas.android.com/apk/res/com.myown.smthg"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:orientation="vertical"
      android:background="@drawable/background_white_gray"
      android:paddingLeft="16dip"
      android:paddingRight="16dip"
      android:isScrollContainer="true" >

  <TextView
     android:id="@+id/TextViewWaitReason"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:gravity="center"
         android:paddingTop="12dip"
         android:paddingLeft="12dip"
         android:paddingRight="12dip"
         android:textColor="#343434"
         android:textSize="20sp" />

  <com.myown.smthg.util.GIFView
     custom:resourceId="@drawable/progress_bar"
     android:id="@+id/progressGIF"
     android:layout_below="@id/TextViewWaitReason"
     android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:gravity="center"
         android:paddingTop="0dip"
         android:paddingLeft="24dip"
         android:paddingRight="24dip"
         android:paddingBottom="16dip" />
  </RelativeLayout>

然后是GIFView.java的代码:

public class GIFView extends View {
// The resource id of the GIF
private int mResourceId;

// Movie to be shown
private Movie mMovie;
private long mStartTime;

// Size of this View
private int mHeight, mWidth;

/**
 * Constructor
 * 
 * @param context
 * @param attributes
 */
public GIFView( Context context, AttributeSet attributes ) {
    super( context, attributes );

    // Get the attribute for resource id
    TypedArray t = context.getTheme().obtainStyledAttributes( 
            attributes, R.styleable.GIFView, 0, 0 );

    mResourceId = -1;
    mMovie = null;
    mStartTime = 0;
    mHeight = 0;
    mWidth = 0;

    // This call might fail
    try
    {
        mResourceId = t.getResourceId ( R.styleable.GIFView_resourceId, -1 );

        mMovie = Movie.decodeStream( context.getResources().openRawResource( mResourceId ) );
        if( mMovie != null )
        {
            mWidth = mMovie.width();
            mHeight = mMovie.height();
        }
    }
    finally
    {
        t.recycle();
    }
}

@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{   
    final int desiredHSpec = MeasureSpec.makeMeasureSpec( mHeight, MeasureSpec.EXACTLY );
    final int desiredWSpec = MeasureSpec.makeMeasureSpec( mWidth, MeasureSpec.EXACTLY );
    setMeasuredDimension( desiredWSpec, desiredHSpec );

    super.onMeasure( desiredWSpec, desiredHSpec );
}

@Override
protected void onDraw( Canvas canvas )
{
    super.onDraw( canvas );

    // Return if we have no movie
    if( mMovie == null ) return;

    // Catch the time now
    long now = System.currentTimeMillis();

    // Catch the start time if needed
    if( mStartTime == 0 ) mStartTime = now;

    int relTime = (int)( (now- mStartTime) % mMovie.duration() );
    mMovie.setTime( relTime );
    mMovie.draw( canvas, 0, 0 );

    this.invalidate();
}

如果这很重要,那么 Activity 构造函数如下所示:

super.onCreate( savedInstanceState );
// Set the content view
setContentView( R.layout.layout_wait );

// Get the String to be shown
Intent intent = getIntent();
String waitStr = intent.getStringExtra( myService.EXTRA_TEXT );

// Set the done text
TextView t = (TextView)findViewById( R.id.TextViewWaitReason );
if( t != null && waitStr != null )
    t.setText( waitStr );

那么,为什么我不能让动画在文本下方和屏幕中央运行?如果我之前能弄清楚,我会在这里发布修复...它与 onMeasure() 方法有关,因为覆盖它会改变一切。

关于 fragment 的免责声明:我不会在 onDraw() 的末尾留下 invalidate()...

Br, 提姆

最佳答案

好的,感谢所有有时间研究这个问题的人。我也这样做了,正如我所料,解决方案相当简单......

问题是我没有为我的 View 声明正确的大小,只是声明它与 GIF 一样宽。因此,修复 onMeasure 以设置尺寸以获取所有给定的宽度,然后考虑填充并绘制到正确的位置固定一切。固定代码 fragment :

新变量:

private int mDrawLeftPos;

固定在测量上

@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
{   
    int p_top = this.getPaddingTop(), p_bottom = this.getPaddingBottom();

    // Calculate new desired height
    final int desiredHSpec = MeasureSpec.makeMeasureSpec( mHeight + p_top + p_bottom , MeasureSpec.EXACTLY );

    setMeasuredDimension( widthMeasureSpec, desiredHSpec );
    super.onMeasure( widthMeasureSpec, desiredHSpec );

    // Update the draw left position
    mDrawLeftPos = Math.max( ( this.getWidth() - mWidth ) / 2, 0) ;
}

固定onDraw

@Override
protected void onDraw( Canvas canvas )
{
    super.onDraw( canvas );

    // Return if we have no movie
    if( mMovie == null ) return;

    // Catch the time now
    long now = System.currentTimeMillis();

    // Catch the start time if needed
    if( mStartTime == 0 ) mStartTime = now;

    int relTime = (int)( (now- mStartTime) % mMovie.duration() );
    mMovie.setTime( relTime );
    mMovie.draw( canvas, mDrawLeftPos, this.getPaddingTop() );
}

Br, 提姆

关于java - 安卓用户界面 : Positioning of custom GIF View within RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12918543/

相关文章:

java - 确定方法调用是否在多个线程上执行

Android Studio 标记为错误的 Android SQLite 查询

android - Android 库中的字体

java - ExpandableListView 子数组元素到带有 Intent 的新页面

java - Application Client <-> EJB calls round-trip 似乎很慢

java - SpringSession DefaultCookieSerializer.setJvmRoute 有效,但 HttpServletRequest 没有所需的 jvmRoute

java - Vaadin 14 slider

android - 在 Android 中通过 Settings.Global 撤消设置代理

java - 如果某些东西已被弃用,我仍然可以使用它吗?

java - 单击 jmenu 时调用 actionperformed 方法