I wanted to add a method to blink the imageViewwith an animation. I want to blink the implementation just by calling
.blink ()` without giving it to the user as much as possible.
Therefore, implement a new class that inherits ʻImageView` and add a new method.
BlinkableImageView.java
package com.hoge_company.fooapp.widget;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
class BlinkableImageView extends AppCompatImageView {
public BlinkableImageView(Context context) {
super(context);
initialize();
}
public BlinkableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public BlinkableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
protected void initialize() {
//If you have any preparations here
}
public void blink(int durationMillis) {
//Implementation for blinking
// getContext()Can get Context with
}
}
Override three constructors with public
.
I forgot public
and spent a lot of time.
The implementation for blinking is out of the main subject, so I will not write it here.
You can write in xml like this. The attributes are appropriate.
activity_main.xml
<com.hoge_company.fooapp.widget.BlinkableImageView
android:id="@+id/foo_image"
android:visibility="invisible"
android:scaleType="fitCenter"
android:adjustViewBounds="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
In some cases, we don't override all the constructors, I'm worried if it's okay in the future, so I decided to override everything.
Recommended Posts