Android4.2.2 Gallery2源码分析(2)——发现Gallery.java

上文中,main.xml是我直接提出来的,并没有说明是怎么找到它的,现在说明发现它的理由:

一般我们分析界面布局会用到hierarchyviewer这个工具,从工具中,我们对应到视图,最主要的视图id我们找到了"gl_root_view",这一点在上一节中有说明。在Source insight中搜索这个id,我们找到了layout/Gl_root_group.xml:

  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">  
  2.     <com.android.gallery3d.ui.GLRootView  
  3.             android:id="@+id/gl_root_view"  
  4.             android:layout_width="match_parent"  
  5.             android:layout_height="match_parent"/>  
  6.     <View android:id="@+id/gl_root_cover"  
  7.             android:layout_width="match_parent"  
  8.             android:layout_height="match_parent"  
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <com.android.gallery3d.ui.GLRootView
            android:id="@+id/gl_root_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    <View android:id="@+id/gl_root_cover"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
  1. ...  
...

注意到这个layout的根标签为<merge>,因此这个layout是由别的layout整合使用的,再次搜索这个layout,找到了Main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:id="@+id/gallery_root"  
  4.         android:orientation="vertical"  
  5.         android:layout_width="match_parent"  
  6.         android:layout_height="match_parent">  
  7.     <include layout="@layout/gl_root_group"/>  
  8.     <FrameLayout android:id="@+id/header"  
  9.             android:visibility="gone"  
  10.             android:layout_alignParentTop="true"  
  11.             android:layout_width="match_parent"  
  12.             android:layout_height="wrap_content"/>  
  13.     <FrameLayout android:id="@+id/footer"  
  14.             android:visibility="gone"  
  15.             android:layout_alignParentBottom="true"  
  16.             android:layout_alignParentLeft="true"  
  17.             android:layout_alignParentRight="true"  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="wrap_content"/>  
  20. </RelativeLayout>  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gallery_root"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <include layout="@layout/gl_root_group"/>
    <FrameLayout android:id="@+id/header"
            android:visibility="gone"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    <FrameLayout android:id="@+id/footer"
            android:visibility="gone"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</RelativeLayout>

综合上节中用hierarchyviewer看到的界面布局,因此这个Main.xml就是Gallery的主视图入口,在Gallery工程中搜索调用这个Main.xml的Activity。找到了Gallery.java这个类,从名字上应该有所发觉,这个类有点像我们以往所见的ActivityMain.java。因此Gallery.java是这个布局所对应的activity。

我们先抛开GLRootView.java这个视图,先从activity入手分析一下Gallery.java这个类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package e gallery; import android app Activity; import android os Bundle; import android view LayoutInflater; import android view View; import android view ViewGroup; import android view ViewGroup MarginLayoutParams; import android widget AdapterView; import android widget AdapterView OnItemSelectedListener; import android widget BaseAdapter; import android widget Gallery; import android widget ImageView; import e gallery R; public class MainActivity extends Activity implements OnItemSelectedListener { Gallery gallery; ImageView main imageView; ImageView picture imageView; Gesture+ gesture; int[] ids1 { R drawable b R drawable c R drawable d R drawable f R drawable g }; @Override protected void onCreate Bundle savedInstanceState { super onCreate savedInstanceState ; setContentView R layout main ; main imageView ImageView findViewById R id main imageView ; gallery Gallery findViewById R id gallery ; gallery setOnItemSelectedListener this ; 怎么设置从中间开始 gallery setAdapter new MyAdapter ; } class MyAdapter extends BaseAdapter{ @Override public int getCount { return Integer MAX VALUE; } @Override public Object getItem int position { return null; } @Override public long getItemId int position { return 0; } @Override public View getView int position View convertView ViewGroup parent { LayoutInflater layoutInflater LayoutInflater from MainActivity this ; convertView layoutInflater inflate R layout picture null ; picture imageView ImageView convertView findViewById R id picture imageView ; picture imageView setImageResource ids1[position % 5] ; return convertView; } } @Override public void onItemSelected AdapterView< > parent View view int position long id { main imageView setImageResource ids1[position % 5] ; } @Override public void onNothingSelected AdapterView< > parent { } }">package e gallery; import android app Activity; import android os Bundle; import android view LayoutInflater; import android view View; import android view ViewGroup; import android view ViewGroup MarginLayoutParams; import android widget AdapterView; import android widget AdapterView OnItemSelecte [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值