Created
May 16, 2013 07:18
-
-
Save dzwillpower/5589975 to your computer and use it in GitHub Desktop.
Used to draw a custom overly over a target view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.ipanel.android.widget; | |
import cn.ipanel.android.Logger; | |
import android.animation.TypeEvaluator; | |
import android.animation.ValueAnimator; | |
import android.annotation.TargetApi; | |
import android.app.Activity; | |
import android.graphics.Rect; | |
import android.os.Build; | |
import android.view.View; | |
import android.view.ViewGroup.LayoutParams; | |
import android.view.ViewGroup.MarginLayoutParams; | |
import android.view.ViewParent; | |
import android.view.animation.Animation; | |
import android.view.animation.Transformation; | |
import android.widget.FrameLayout; | |
import android.widget.ImageView; | |
/** | |
* Used to draw a custom overly over a target view | |
* | |
* @author Zexu | |
* | |
*/ | |
public class ViewFrameIndicator { | |
private ImageView mImg; | |
public ViewFrameIndicator(Activity context) { | |
mImg = new ImageView(context); | |
context.addContentView(mImg, new LayoutParams( | |
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); | |
mImg.setVisibility(View.INVISIBLE); | |
} | |
public void setFrameResouce(int resid) { | |
mImg.setBackgroundResource(resid); | |
} | |
public ImageView getImageView() { | |
return mImg; | |
} | |
/** | |
* 如果是 .9.png, 会自动设置padding, 调用此方法会覆盖.9.png的padding | |
* | |
* @param left | |
* @param top | |
* @param right | |
* @param bottom | |
*/ | |
public void setPadding(int left, int top, int right, int bottom) { | |
mImg.setPadding(left, top, right, bottom); | |
} | |
public void setFrameColor(int color) { | |
mImg.setBackgroundColor(color); | |
} | |
public void moveFrmaeTo(View v) { | |
moveFrameTo(v, true, false); | |
} | |
public int getLeftInScrren(View v){ | |
ViewParent vp = v.getParent(); | |
if(vp instanceof View){ | |
return (int) (v.getLeft() - v.getScrollX()+getLeftInScrren((View)vp)); | |
} | |
return v.getLeft() - v.getScrollX(); | |
} | |
public int getTopInScrren(View v){ | |
ViewParent vp = v.getParent(); | |
if(vp instanceof View){ | |
return v.getTop() - v.getScrollY()+getTopInScrren((View) vp); | |
} | |
return v.getTop() - v.getScrollY(); | |
} | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
public void moveFrameTo(View v, boolean animated, boolean hideFrame) { | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) | |
return; | |
View follower = mImg; | |
if (v != null) { | |
int[] xy = new int[2]; | |
// v.getLocationOnScreen(xy); | |
xy[0] = getLeftInScrren(v); | |
xy[1] = getTopInScrren(v); | |
Rect r = new Rect(); | |
v.getFocusedRect(r); | |
xy[0] += r.left; | |
xy[1] += r.top; | |
int w = r.width(); | |
int h = r.height(); | |
Logger.d(String.format("follower: %d x %d, %f, %f",follower.getWidth(), follower.getHeight(),follower.getScaleX(), follower.getScaleY())); | |
Logger.d(String.format("class: %s, x: %d, y: %d, w:%d,h:%d", v.getClass().getName(), xy[0], xy[1], w, h)); | |
MarginLayoutParams start = (MarginLayoutParams) follower.getLayoutParams(); | |
FrameLayout.LayoutParams end = new FrameLayout.LayoutParams(w | |
+ follower.getPaddingLeft() + follower.getPaddingRight(), h | |
+ follower.getPaddingTop() + follower.getPaddingBottom()); | |
end.leftMargin = xy[0] - follower.getPaddingLeft(); | |
end.topMargin = xy[1] - follower.getPaddingTop(); | |
boolean wasVisible = follower.getVisibility() == View.VISIBLE; | |
if (hideFrame) { | |
follower.setVisibility(View.INVISIBLE); | |
} else { | |
follower.setVisibility(View.VISIBLE); | |
} | |
if (animated && !hideFrame && wasVisible){ | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { | |
ValueAnimator.ofObject(new LayoutEvaluator(follower), start, end).setDuration(100).start(); | |
} else { | |
LayoutAnimation ani = new LayoutAnimation(follower, start, | |
end); | |
ani.setDuration(100); | |
follower.setAnimation(ani); | |
} | |
}else | |
follower.setLayoutParams(end); | |
} else { | |
follower.setVisibility(View.INVISIBLE); | |
} | |
} | |
class LayoutAnimation extends Animation{ | |
View follower; | |
MarginLayoutParams startValue; | |
MarginLayoutParams endValue; | |
public LayoutAnimation (View v, MarginLayoutParams start, MarginLayoutParams end){ | |
this.follower = v; | |
this.startValue = start; | |
this.endValue = end; | |
} | |
@Override | |
protected void applyTransformation(float fraction, | |
Transformation t) { | |
int width = (int) (startValue.width + fraction | |
* (endValue.width - startValue.width)); | |
int height = (int) (startValue.height + fraction | |
* (endValue.height - startValue.height)); | |
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(width, | |
height); | |
p.leftMargin = (int) (startValue.leftMargin + fraction | |
* (endValue.leftMargin - startValue.leftMargin)); | |
p.topMargin = (int) (startValue.topMargin + fraction | |
* (endValue.topMargin - startValue.topMargin)); | |
follower.setLayoutParams(p); | |
} | |
} | |
class LayoutEvaluator implements TypeEvaluator<MarginLayoutParams> { | |
View follower; | |
public LayoutEvaluator(View v) { | |
this.follower = v; | |
} | |
@Override | |
public MarginLayoutParams evaluate(float fraction, | |
MarginLayoutParams startValue, MarginLayoutParams endValue) { | |
int width = (int) (startValue.width + fraction | |
* (endValue.width - startValue.width)); | |
int height = (int) (startValue.height + fraction | |
* (endValue.height - startValue.height)); | |
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(width, | |
height); | |
p.leftMargin = (int) (startValue.leftMargin + fraction | |
* (endValue.leftMargin - startValue.leftMargin)); | |
p.topMargin = (int) (startValue.topMargin + fraction | |
* (endValue.topMargin - startValue.topMargin)); | |
follower.setLayoutParams(p); | |
return p; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment