Android:在自定义视图

时间:2016-10-07 04:15:41

标签: java android override android-custom-view custom-view

所以我在自定义视图中遇到了这个问题。我正在尝试使用无限滚动动画创建自定义RelativeLayout。为实现这一目标,我创建了一个布局backgroundscrollrelativelayout.xml,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainTile"/>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/topTile" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/leftTile" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/diagonalTile" />

</RelativeLayout>

我们的想法是ImageView将在动画更新回调中转换它们的位置。

我已经创建了BackgroundScrollRelativeLayout.java,如此:

public class BackgroundScrollRelativeLayout extends RelativeLayout {
final int layoutToUse = R.layout.backgroundscrollrelativelayout;
final int mainTileId = R.id.mainTile;
final int leftTileId = R.id.leftTile;
final int topTileId = R.id.topTile;
final int diagonalTileId = R.id.diagonalTile;

final float valueStart = 0.0f;
final float valueEnd = 1.0f;
final long animationDuration = 50000L;

private Context mContext;
private ValueAnimator scrollAnimator;

private ImageView mainTile;
private ImageView leftTile;
private ImageView topTile;
private ImageView diagonalTile;


public BackgroundScrollRelativeLayout(Context context) {
    super(context);
    mContext = context;
    acquireViewsInLayout();
    initializeAnimator();
    scrollAnimator.start();
}

public BackgroundScrollRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    acquireViewsInLayout();
    initializeAnimator();
    scrollAnimator.start();
}


public BackgroundScrollRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
    acquireViewsInLayout();
    initializeAnimator();
    scrollAnimator.start();
}

@Override
public void setBackgroundColor(int color) {
    // Not supported
}

@Override
public void setBackgroundResource(int resid) {
    // Not supported
}


@Override
public void setBackground(Drawable background) {
    mainTile.setBackground(background);
    leftTile.setBackground(background);
}

/*
@Override
public void setBackgroundDrawable(Drawable background) {
    //super.setBackgroundDrawable(background);
    //mainTile.setBackground(background);
    //leftTile.setBackground(background);
}*/

// Intent: Inflate the layout associated with this View
private void inflateLayout(){
    // TO inflateLayout, we connect the inflater to context, then we call inflate the layout associated
    // with this view

    //LayoutInflater inflater = LayoutInflater.from(mContext);
    //inflater.inflate(layoutToUse, this);
    inflate(getContext(), layoutToUse, this);
}

// Intent: Find all Views in Layout
private void findAllViewsById(){
    mainTile = (ImageView)this.findViewById(mainTileId);
    leftTile = (ImageView)this.findViewById(leftTileId);
    topTile = (ImageView)this.findViewById(topTileId);
    diagonalTile = (ImageView)this.findViewById(diagonalTileId);
}

// Intent: Concretely acquire all Views in Layout
private void acquireViewsInLayout(){
    // TO acquireViewsInLayout, we inflate the layout,
    // then we find the view of each known view id and save the view
    inflateLayout();
    findAllViewsById();
}

// Intent: Initialize animator properties
private void initializeAnimator(){
    // TO initializeAnimator, we set how the animator will keep track of animation,
    // then we set the animation repeat type, then we set the type of interpolation,
    // then we set the animation duration, then we apply animation update listener
    scrollAnimator = ValueAnimator.ofFloat(valueStart, valueEnd);
    scrollAnimator.setRepeatCount(ValueAnimator.INFINITE);
    scrollAnimator.setInterpolator(new LinearInterpolator());
    scrollAnimator.setDuration(animationDuration);
    addScrollAnimatorUpdateListener();
}

// Intent: Add an update listener to the scroll animator
private void addScrollAnimatorUpdateListener(){
    // TO addScrollAnimatorUpdateListener, we add an update listener to scroll animator
    scrollAnimator.addUpdateListener( new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            // Do something...
            updateScrollAnimation();
        }
    });

}

private void updateScrollAnimation(){
    float progress = (float)scrollAnimator.getAnimatedValue();
    float widthOfTile = mainTile.getWidth();

    float moveInXAxis = widthOfTile * progress;

    mainTile.setTranslationX(moveInXAxis);
    leftTile.setTranslationX(moveInXAxis - widthOfTile);


    // Ignore the rest for now
    topTile.setTranslationY(-1.0f);
    diagonalTile.setTranslationX(-1.0f);
    diagonalTile.setTranslationY(-1.0f);

}



}

我在活动中使用我的自定义视图:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.martianstudio.adivinaque.AppSettingsActivity">

<com.martianstudio.adivinaque.BackgroundScrollRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_activity_animation_icon" />
</RelativeLayout>

这样做的目的是允许用户使用android:background指定要滚动的背景。我不希望父RelativeLayout将此drawable作为背景。相反,我希望ImageViews将drawable设置为背景。我在自定义视图中覆盖了setBackground,以便只有ImageView将drawable设置为背景而不是RelativeLayout(默认情况下)。

@Override
public void setBackground(Drawable background) {
    mainTile.setBackground(background);
    leftTile.setBackground(background);
}

然而,我收到此错误:

java.lang.NullPointerException at com.martianstudio.adivinaque.BackgroundScrollRelativeLayout.setBackground

这表示mainTile中的setBackground尚未设置(它为空)。在我的findAllViewsById()中,我明确地写了this.mainTile = (ImageView)this.findViewById(mainTileId);,其中mainTileId = R.id.mainTile,我在inflateLayout()中夸大了布局。这两种方法都在类的构造函数中调用。在我看来,由于某些原因,当我像我一样覆盖ImageView方法时,它找不到mainTile setBackground ID。如果我没有覆盖setBackground方法,我没有得到NullPointerException,但是,它默认为默认的setBackground方法,这是我不想要的。我错过了什么吗?

任何帮助将不胜感激:)。谢谢!

0 个答案:

没有答案