先看看效果图:
1. Activity
package demo.invalidate;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private int mRelativeColor = R.color.skyblue;
private int mLinearColor = R.color.wheat;
private int mViewColor = R.color.seagreen;
private TextView mRelativeOutput;
private TextView mLinearOutput;
private TextView mCustomviewOutput;
private InvalidateRelativeLayout mRelativeLayout;
private InvalidateLinearLayout mLinearLayout;
private InvalidateView mCustomView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRelativeLayout = (InvalidateRelativeLayout) findViewById(R.id.relative_layout);
mRelativeLayout.setOnClickListener(new RelativeClickListener());
mRelativeLayout.setBackgroundColor( getResources().getColor(mRelativeColor) );
mRelativeOutput = (TextView) findViewById(R.id.relative_ouput);
mLinearLayout = (InvalidateLinearLayout) findViewById(R.id.linear_layout);
mLinearLayout.setOnClickListener(new LinearClickListener());
mLinearLayout.setBackgroundColor( getResources().getColor( mLinearColor) );
mLinearOutput = (TextView) findViewById(R.id.linear_ouput);
mCustomView = (InvalidateView) findViewById(R.id.custom_view);
mCustomView.setOnClickListener(new ViewClickListener());
mCustomView.setBackgroundColor( getResources().getColor( mViewColor) );
mCustomviewOutput = (TextView) findViewById(R.id.view_ouput);
}
private void updateOutput() {
output(mRelativeOutput, "蓝色", mRelativeLayout.getLeft(), mRelativeLayout.getTop());
output(mLinearOutput, "黄色", mLinearLayout.getLeft(), mLinearLayout.getTop());
output(mCustomviewOutput, "绿色", mCustomView.getLeft(), mCustomView.getTop());
}
private void output(TextView pOutput, String pColorName, int pLeft, int pTop) {
pOutput.setText(pColorName + "区域坐标:" + "\ngetLeft() = " + pLeft + "\ngetTop() = " + pTop);
pOutput.setTextColor(Color.RED);
}
private boolean relativeFlag = false;
private class RelativeClickListener implements OnClickListener {
@Override
public void onClick(View v) {
updateOutput();
if (!relativeFlag) {
v.setBackgroundColor(Color.BLUE);
relativeFlag = true;
} else {
v.setBackgroundColor( getResources().getColor( mRelativeColor ) );
relativeFlag = false;
}
}
}
private boolean linearFlag = false;
private class LinearClickListener implements OnClickListener {
@Override
public void onClick(View v) {
updateOutput();
if (!linearFlag) {
v.setBackgroundColor(Color.YELLOW);
linearFlag = true;
} else {
getResources().getColor(R.color.wheat);
v.setBackgroundColor( getResources().getColor( mLinearColor ) );
linearFlag = false;
}
}
}
private boolean viewFlag = false;
private class ViewClickListener implements OnClickListener {
@Override
public void onClick(View v) {
updateOutput();
if (!viewFlag) {
v.setBackgroundColor(Color.GREEN);
viewFlag = true;
} else {
v.setBackgroundColor( getResources().getColor( mViewColor ) );
viewFlag = false;
}
}
}
}
2. 蓝色区域是自定义ViewGroup
package demo.invalidate;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class InvalidateRelativeLayout extends RelativeLayout {
public InvalidateRelativeLayout(Context context) {
super(context);
}
public InvalidateRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InvalidateRelativeLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
}
3. 黄色区域自定义ViewGroup
package demo.invalidate;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewParent;
import android.widget.LinearLayout;
public class InvalidateLinearLayout extends LinearLayout {
public InvalidateLinearLayout(Context context) {
super(context);
}
public InvalidateLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void invalidate(int l, int t, int r, int b) {
Log.e("Test", "LinearLayout invalidate" + " , l = " + l + " , t = "
+ t + " , r = " + r + " , b = " + b);
super.invalidate(l, t, r, b);
}
@Override
public void invalidate() {
Log.e("Test", "LinearLayout invalidate()");
super.invalidate();
}
@Override
public void invalidate(Rect dirty) {
Log.e("Test", "LinearLayout invalidate(Rect dirty)" );
super.invalidate(dirty);
}
@Override
public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
Log.e("Test", "LinearLayout invalidateChildInParent");
return super.invalidateChildInParent(location, dirty);
}
@Override
public void invalidateDrawable(Drawable drawable) {
Log.e("Test", "LinearLayout drawable");
super.invalidateDrawable(drawable);
}
}
4 中间绿色区域是自定义View
package demo.invalidate;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class InvalidateView extends ImageView {
public InvalidateView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void invalidate(int l, int t, int r, int b) {
Log.e("Test", "Button invalidate"
+ " , l = " + l + " , t = " + t + " , r = " + r + " , b = " + b);
super.invalidate(l, t, r, b);
}
@Override
public void invalidate() {
Log.e("Test", "Button invalidate()");
super.invalidate();
}
@Override
public void invalidate(Rect dirty) {
Log.e("Test", "Button invalidate(Rect dirty)");
super.invalidate(dirty);
}
@Override
public void invalidateDrawable(Drawable drawable) {
Log.e("Test", "Button invalidateDrawable(Drawable drawable)");
super.invalidateDrawable(drawable);
}
}
5 配置文件
<demo.invalidate.InvalidateRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="@+id/relative_layout">
<demo.invalidate.InvalidateLinearLayout
android:id="@+id/linear_layout"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_centerInParent="true">
<demo.invalidate.InvalidateView
android:id="@+id/custom_view"
android:layout_width="90dip"
android:layout_height="90dip"
android:layout_gravity="center_vertical"
android:layout_marginLeft="55dip"/>
</demo.invalidate.InvalidateLinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/relative_ouput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/linear_ouput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/view_ouput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</demo.invalidate.InvalidateRelativeLayout>