Android Open Source - lastfm-android-music-discovery Dynamic Image View






From Project

Back to project page lastfm-android-music-discovery.

License

The source code is released under:

Apache License

If you think the Android project lastfm-android-music-discovery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mikeladze.musicdiscovery.view.gridview;
//from  w  w  w  .j  a  v a 2s.  c o  m
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class DynamicImageView extends ImageView {
  
  private ImageChangeListener imageChangeListener;
  private boolean scaleToWidth = false;
  
  public DynamicImageView(Context context) {
    super(context);
    init();
  }
  
  public DynamicImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }
  
  public DynamicImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }
  
  private void init() {
    this.setScaleType(ScaleType.CENTER_INSIDE);
  }
  
  @Override
  public void setImageBitmap(Bitmap bm) {
    super.setImageBitmap(bm);
    if (imageChangeListener != null) {
      imageChangeListener.changed((bm == null));
    }
  }
  
  @Override
  public void setImageDrawable(Drawable d) {
    super.setImageDrawable(d);
    if (imageChangeListener != null) {
      imageChangeListener.changed((d == null));
    }
  }
  
  @Override
  public void setImageResource(int id) {
    super.setImageResource(id);
  }
  
  public interface ImageChangeListener {
    
    void changed(boolean isEmpty);
  }
  
  public ImageChangeListener getImageChangeListener() {
    return imageChangeListener;
  }
  
  public void setImageChangeListener(ImageChangeListener imageChangeListener) {
    this.imageChangeListener = imageChangeListener;
  }
  
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    
    /**
     * if both width and height are set scale width first. modify in future if necessary
     */
    
    if (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST) {
      scaleToWidth = true;
    } else if (heightMode == MeasureSpec.EXACTLY || heightMode == MeasureSpec.AT_MOST) {
      scaleToWidth = false;
    } else {
      throw new IllegalStateException("width or height needs to be set to match_parent or a specific dimension");
    }
    
    if (getDrawable() == null || getDrawable().getIntrinsicWidth() == 0) {
      // nothing to measure
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      return;
    } else {
      if (scaleToWidth) {
        int iw = this.getDrawable()
                .getIntrinsicWidth();
        int ih = this.getDrawable()
                .getIntrinsicHeight();
        int heightC = width * ih / iw;
        if (height > 0) {
          if (heightC > height) {
            // dont let hegiht be greater then set max
            heightC = height;
            width = heightC * iw / ih;
          }
        }
        
        this.setScaleType(ScaleType.CENTER_CROP);
        setMeasuredDimension(width, heightC);
        
      } else {
        // need to scale to height instead
        int marg = 0;
        if (getParent() != null) {
          if (getParent().getParent() != null) {
            marg += ((RelativeLayout) getParent().getParent()).getPaddingTop();
            marg += ((RelativeLayout) getParent().getParent()).getPaddingBottom();
          }
        }
        
        int iw = this.getDrawable()
                .getIntrinsicWidth();
        int ih = this.getDrawable()
                .getIntrinsicHeight();
        
        width = height * iw / ih;
        height -= marg;
        setMeasuredDimension(width, height);
      }
      
    }
  }
  
}




Java Source Code List

com.mikeladze.musicdiscovery.album.activity.AlbumActivity.java
com.mikeladze.musicdiscovery.album.adapter.LastFMTrack.java
com.mikeladze.musicdiscovery.album.adapter.OnTrackClickListener.java
com.mikeladze.musicdiscovery.album.adapter.TrackListAdapter.java
com.mikeladze.musicdiscovery.album.fragment.AlbumInfoFragment.java
com.mikeladze.musicdiscovery.album.fragment.AlbumTracksFragment.java
com.mikeladze.musicdiscovery.artist.activity.ArtistActivity.java
com.mikeladze.musicdiscovery.artist.fragment.ArtistAlbumsFragment.java
com.mikeladze.musicdiscovery.artist.fragment.ArtistInfoFragment.java
com.mikeladze.musicdiscovery.artist.fragment.SimilarArtistsFragment.java
com.mikeladze.musicdiscovery.base.activity.BaseFragmentActivity.java
com.mikeladze.musicdiscovery.base.adapter.SectionsPagerAdapter.java
com.mikeladze.musicdiscovery.base.fragment.BaseTabFragment.java
com.mikeladze.musicdiscovery.base.fragment.ImageGridViewFragment.java
com.mikeladze.musicdiscovery.http.LastFMResult.java
com.mikeladze.musicdiscovery.http.RestClient.java
com.mikeladze.musicdiscovery.main.activity.MainActivity.java
com.mikeladze.musicdiscovery.main.fragment.HypedArtistsFragment.java
com.mikeladze.musicdiscovery.main.fragment.TopArtistsFragment.java
com.mikeladze.musicdiscovery.search.activity.SearchResultsActivity.java
com.mikeladze.musicdiscovery.search.adapter.SearchResultsAdapter.java
com.mikeladze.musicdiscovery.util.IntentUtils.java
com.mikeladze.musicdiscovery.view.gridview.DynamicImageView.java
com.mikeladze.musicdiscovery.view.gridview.ImageGridAdapter.java
com.mikeladze.musicdiscovery.view.gridview.loader.FileCache.java
com.mikeladze.musicdiscovery.view.gridview.loader.ImageLoader.java
com.mikeladze.musicdiscovery.view.gridview.loader.MemoryCache.java
com.mikeladze.musicdiscovery.view.gridview.loader.Utils.java