重写Android ----ViewGroup
ViewGroup
上文提到了如何重写View与认识 点击打开链接
现在来重写ViewGroup,我看过许多网上重写的ViewGroup大多都是用ViewPager等来填充ViewGroup的,我这用下动态创建的方式填充。
实验结果:
1.准备:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.com.example.reViewGroup.reViewGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content"> </com.example.com.example.reViewGroup.reViewGroup> </LinearLayout>
2.MainActivity
public class MainActivity extends Activity { ViewGroup view; List<Integer> imgList= Arrays.asList(R.drawable.message_icon_chengkao, R.drawable.message_icon_gaokao, R.drawable.message_icon_shekao, R.drawable.message_icon_yanjiusheng, R.drawable.message_icon_zikao); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reviewgroup); view=(ViewGroup) findViewById(R.id.group); for (int i:imgList){ ImageView iv=new ImageView(this); ViewGroup.LayoutParams pa=new ViewGroup.LayoutParams((getPOP()-4*reViewGroup.WIDTH_SPACE)/3,(getPOP()-4*reViewGroup.WIDTH_SPACE)/3); iv.setLayoutParams(pa); iv.setImageResource(i); iv.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"监听事件",Toast.LENGTH_LONG).show(); } }); view.addView(iv); } } private int getPOP() { WindowManager wm = (WindowManager)getApplicationContext() .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics=new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); //设置PopupWindow的宽度和高度 int popWidth=outMetrics.widthPixels; return popWidth; } }3.重写ViewGroup
public class reViewGroup extends ViewGroup { /** * 单行最多图片数 */ public final static int LINE_MAX_COUNT = 3; public final static int WIDTH_SPACE = 20; public final static int HEIGHT_SPACE = 20; int viewCount=0; int childWidth=0; public reViewGroup(Context context) { this(context,null); } public reViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } /** * 为子View设置其宽高 * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int w=0; int h=0; viewCount=getChildCount(); Log.i("info---viewCount",""+viewCount); if (viewCount==0){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); return; } for (int i = 0; i <viewCount; i++) { View child = this.getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); childWidth=child.getMeasuredWidth(); if (i==0||i==2){ w=w+WIDTH_SPACE+child.getMeasuredWidth()+WIDTH_SPACE; }else if (i==1){ w=w+WIDTH_SPACE+child.getMeasuredWidth(); } } if(viewCount<=LINE_MAX_COUNT){ h=HEIGHT_SPACE+childWidth; }else{ h=(viewCount/LINE_MAX_COUNT+1)*(childWidth+HEIGHT_SPACE); } Log.i("info---wh","w"+w+"h"+h); setMeasuredDimension(w,h); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int h=HEIGHT_SPACE; int w=0; if (getChildCount()==0)return; for(int i=0;i<viewCount;i++){ View child=this.getChildAt(i); Log.i("info---height",""+child.getMeasuredHeight()); w=(i%LINE_MAX_COUNT)*(WIDTH_SPACE+childWidth)+WIDTH_SPACE; child.layout(w,h,w+childWidth,h+child.getMeasuredHeight()); if ((i+1)%3==0){ h=h+HEIGHT_SPACE+child.getMeasuredHeight(); } } } @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } }
现在项目来了,去找大佬去了,后面再补充。。。
好了,蹭着这时有空,赶快把这搞定。。
其实对于重写View来说,无非就是对canvas,path,paint,bitmap...的运用,这些就自己去学,没什么好说的。
对于ViewGroup来说,重要的就是其onMeasure与onLayout的重写,但是有点需要注意,就是在重写onMeasure的时候,要记得调用measureChild()或child.measure,这是告诉ViewGroup你有子View且他的大小是多少。