[JAVA] [Android] Inherit ImageView to create a new class

Summary

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.

Implementation

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

reference

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

[Android] Inherit ImageView to create a new class
Create a temporary class with new Object () {}
[Rails] rails new to create a database with PostgreSQL
How to create a class that inherits class information
How to create a method
[Android] Create a calendar using GridView
Preparing to create a Rails application
Android Development-Try to display a dialog-
Create a new app in Rails
[Java] How to create a folder
Try to create a server-client app
[Kotlin / Android] Create a custom view
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Easy way to create a mapping class when using the API
[Rails6] Create a new app with Rails [Beginner]
How to add a new hash / array
How to create a Maven repository for 2020
Android Easily give a "press" to a button
[Swift5] How to create a splash screen
[rails] How to create a partial template
[Rails 5] Create a new app with Rails [Beginner]
Have a tool to create and open a new canvas in Mac preview
[Android] I want to create a ViewPager that can be used for tutorials
A note about adding Junit 4 to Android Studio
How to create a database for H2 Database anywhere
Transform from a normal class to a lambda expression
[Android] Create a sliding menu without using NavigationView
[Rails] How to create a graph using lazy_high_charts
[Android] How to convert a character string to resourceId
Try to create a bulletin board in Java
How to create pagination for a "kaminari" array
[Android] Implement a function to display passwords quickly
How to convert a solidity contract to a Java contract class
I tried to create a LINE clone app
How to create a theme in Liferay 7 / DXP
[1st] How to create a Spring-MVC framework project
How to easily create a pull-down in Rails
[Android] Two ways to get a Bluetooth Adapter
[Rails] How to create a Twitter share button
Bitmap too large to be uploaded into a texture trying to set a large Bitmap in Android ImageView
[Introduction] Try to create a Ruby on Rails application
3. Create a database to access from the web module
How to create a Java environment in just 3 seconds
I want to call a method of another class
[Rails] How to create a signed URL for CloudFront
[Android / Java] Set up a button to return to Fragment
Reasons to include ActiveModel :: Model to create a Form object
How to create a JDBC URL (Oracle Database, Thin)
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
If there is a state transition, let's create a State class
What to do if you accidentally create a model
A memorandum when trying to create a GUI using JavaFX
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
Create a Docker container to convert EPS to PGF source
How to create and execute method, Proc, Method class objects
[Apple Subscription Offer] How to create a promotional offer signature
[Introduction to Android application development] Let's make a counter
Item 25: Limit source files to a single top-level class
[Java] How to start a new line with StringBuilder