按比例放大图片,不拉伸失真
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AspectRatioImageView extends ImageView {
public AspectRatioImageView(Context context) {
super(context);
}
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}
也试试下面这个:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class RatioImageView extends ImageView {
private int originalWidth;
private int originalHeight;
public RatioImageView(Context context) {
this(context, null);
}
public RatioImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RatioImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setOriginalSize(int originalWidth, int originalHeight) {
this.originalWidth = originalWidth;
this.originalHeight = originalHeight;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (originalWidth > 0 && originalHeight > 0) {
float ratio = (float) originalWidth / (float) originalHeight;
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (width > 0) {
height = (int) ((float) width / ratio);
} else if (height > 0) {
width = (int) ((float) height * ratio);
}
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.laowch.githubtrends.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
/**
* Created by lao on 3/4/14.
*/
public class AsyncImageView extends ImageView {
String url;
ImageLoadingListener imageLoadingListener;
public AsyncImageView(final Context context) {
super(context);
init();
}
public AsyncImageView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
public AsyncImageView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
init();
}
public void setImageLoadingListener(ImageLoadingListener listener) {
this.imageLoadingListener = listener;
}
protected void init() {
}
public void loadImage(final String imageUrl) {
if (imageUrl != null && url != null) {
if (Uri.parse(imageUrl).getPath().equals(Uri.parse(url).getPath())) {
return;
}
}
url = imageUrl;
executeLoadImage();
}
protected void executeLoadImage() {
if (TextUtils.isEmpty(url)) {
setImageResource(R.drawable.image_loading_resource);
} else {
DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.image_loading_resource)
.cacheInMemory(true)
.cacheOnDisk(true)
.displayer(new SimpleBitmapDisplayer());
DisplayImageOptions options = builder.build();
ImageLoader.getInstance().displayImage(url, this, options, imageLoadingListener);
}
}
}
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
}
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class SquareLayout extends RelativeLayout {
public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareLayout(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
public class MultiStateView extends ImageView
implements View.OnClickListener {
private SparseIntArray mStateMap;
private ArrayList<Integer> mStateList;
private int mCurState;
private IStateChangeListener mListener;
public MultiStateView(Context context) {
super(context);
init();
}
public MultiStateView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MultiStateView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mStateMap = new SparseIntArray();
mStateList = new ArrayList<>();
setOnClickListener(this);
}
public void addStateAndImage(final int state, final int resId) {
post(new Runnable() {
@Override
public void run() {
mStateMap.put(state, resId);
mStateList.add(state);
if (mStateList.size() == 1) {
mCurState = state;
show(false);
}
}
});
}
public void show(final int state) {
if (mCurState == state) return;
post(new Runnable() {
@Override
public void run() {
mCurState = state;
show(false);
}
});
}
private void show(boolean shouldNotify) {
int id = mStateMap.get(mCurState);
if (id == 0) return;
setImageResource(id);
if (shouldNotify && mListener != null) {
mListener.onStateChange(mCurState);
}
}
public void setOnStateChangeListener(IStateChangeListener listener) {
this.mListener = listener;
}
@Override
public void onClick(View v) {
int size = mStateList.size();
for (int i = 0; i < size; i++) {
if (mCurState == mStateList.get(i)) {
mCurState = mStateList.get((i + 1) % size);
break;
}
}
show(true);
}
public interface IStateChangeListener {
void onStateChange(int state);
}
}
用法:
实现implements MultiStateView.IStateChangeListener接口,重写onStateChange(int state)
......
mPlayModeView = (MultiStateView) findViewById(R.id.play_mode);
......
private void initPlayMode() {
mPlayModeView.setOnStateChangeListener(this);
mPlayModeView.addStateAndImage(SongManager.STATE_ALL, R.drawable.play_all);
mPlayModeView.addStateAndImage(SongManager.STATE_RANDOM, R.drawable.play_random);
mPlayModeView.addStateAndImage(SongManager.STATE_SINGLE, R.drawable.play_single);
int state = SharedUtils.getInt(this, Constants.KEY_PLAY_MODE, SongManager.STATE_ALL);
mPlayModeView.show(state);
}
.......
@Override
public void onStateChange(int state) {
switch (state) {
case SongManager.STATE_ALL:
ToastUtils.show(this, "顺序播放");
SharedUtils.saveInt(this, Constants.KEY_PLAY_MODE, state);
SongManager.with(this).initPlayList();
break;
case SongManager.STATE_RANDOM:
ToastUtils.show(this, "随机播放");
SharedUtils.saveInt(this, Constants.KEY_PLAY_MODE, state);
SongManager.with(this).initPlayList();
break;
case SongManager.STATE_SINGLE:
ToastUtils.show(this, "单曲循环");
SharedUtils.saveInt(this, Constants.KEY_PLAY_MODE, state);
SongManager.with(this).initPlayList();
break;
case SongManager.STATE_PINGING:
mPlayService.play();
break;
case SongManager.STATE_PAUSE:
mPlayService.pause();
break;
}
}