#Android自定義控件1
自定義控件blog的第一篇,學習的大部分内容是通過Android原生控件的組合,來實作一些開發時比較實用且比較常用的各種自定義控件。
##1、自定義控件之仿優酷菜單

定義三個相對布局,用來存放3級動畫圖檔
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xwf.androiddiywidget.MainActivity">
android:id="@+id/rl_level3"
android:layout_width="360dp"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level3">
android:id="@+id/rl_level2"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level2">
android:id="@+id/btn_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_menu"
android:background="@android:color/transparent"
android:layout_centerHorizontal="true"/>
android:id="@+id/rl_level1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level1">
android:id="@+id/btn_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_centerInParent="true"
android:src="@drawable/icon_home"/>
寫一個旋轉動畫的工具類,接受參數是RelativeLayout
public class AnimationUtils {
//定義動畫執行過程
public static int runingAnimation = 0;
public static void rotateOutAnimation(RelativeLayout rl,long offset){
RotateAnimation rotateAnimation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1f);
rotateAnimation.setDuration(1000);
rotateAnimation.setStartOffset(offset);
rotateAnimation.setFillAfter(true);
rotateAnimation.setAnimationListener(new MyAnimationListener());
rl.startAnimation(rotateAnimation);
}
public static void rotateInAnimation(RelativeLayout rl,long offset){
RotateAnimation rotateAnimation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1f);
rotateAnimation.setDuration(1000);
rotateAnimation.setStartOffset(offset);
rotateAnimation.setFillAfter(true);
rotateAnimation.setAnimationListener(new MyAnimationListener());
rl.startAnimation(rotateAnimation);
}
//設定一個動畫監聽
static class MyAnimationListener implements Animation.AnimationListener{
private static final String TAG = "Hsia";
@Override
public void onAnimationStart(Animation animation) {
runingAnimation++;
}
@Override
public void onAnimationEnd(Animation animation) {
runingAnimation--;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
}
##2、viewpager簡單學習
一個Viewpager的Demo,可無限左右切換頁面,沒3秒自動切換頁面。
定義布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.a02viewpagertest.MainActivity">
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="210dp">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignBottom="@+id/vp"
android:background="#55000000">
android:id="@+id/tv_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="哈哈哈哈哈哈哈"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:gravity="center_horizontal"
android:textSize="20sp"/>
android:id="@+id/ll_point"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:orientation="horizontal">
shape選擇器
詳細代碼就3個部分(僞無限循環,自動循環定時器)
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Hsia";
@Bind(R.id.vp)
ViewPager vp;
@Bind(R.id.tv_description)
TextView tvDescription;
@Bind(R.id.ll_point)
LinearLayout llPoint;
private List imageViewList;
private int beforePager = 0;
private String[] imageDescriptionArrays;
//viewpager僞無限循環
private int newPosition;
//當Activity界面不見時不自動切換
private boolean switchPager = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initVPData();
//設定viewpager資料
MyPagerAdapter pagerAdapter = new MyPagerAdapter();
vp.setAdapter(pagerAdapter);
vp.setOnPageChangeListener(new MyOnPageChangeListener());
final int current = Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2 % imageViewList.size());
vp.setCurrentItem(current);
//自動切換頁面
autoSwitchPager();
}
private void autoSwitchPager() {
//用一個定時器
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(switchPager){
vp.setCurrentItem(vp.getCurrentItem()+1);
}
}
});
}
};
timer.schedule(timerTask,3000,3000);
}
@Override
protected void onStart() {
super.onStart();
switchPager = true;
Log.d(TAG, "onStart: ");
}
@Override
protected void onStop() {
super.onStop();
switchPager = false;
Log.d(TAG, "onStop: ");
}
@Override
protected void onDestroy() {
switchPager = false;
super.onDestroy();
}
private void initVPData() {
imageDescriptionArrays = new String[] {
"鞏俐不低俗,我就不能低俗",
"樸樹又回來啦!再唱經典老歌",
"揭秘北京電影如何更新",
"樂視網TV版大派送",
"熱血屌絲的反殺"
};
int[] imageResIDs = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
//用于存放圖檔資料的集合
imageViewList = new ArrayList<>();
for (int i = 0; i < imageResIDs.length; i++) {
ImageView iv = new ImageView(getApplicationContext());
iv.setBackgroundResource(imageResIDs[i]);
imageViewList.add(iv);
//初始化點
View view = new View(getApplicationContext());
view.setBackgroundResource(R.drawable.dot_select);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
if (i > 0){
params.leftMargin = 20;
}
view.setEnabled(false);
view.setLayoutParams(params);
llPoint.addView(view);
}
//設定第一個點和文字描述
tvDescription.setText(imageDescriptionArrays[beforePager]);
llPoint.getChildAt(beforePager).setEnabled(true);
}
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
newPosition = position%imageViewList.size();
ImageView imageView = imageViewList.get(newPosition);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
class MyOnPageChangeListener implements ViewPager.OnPageChangeListener{
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Log.d(TAG, "onPageScrolled: ");
}
@Override
public void onPageSelected(int position) {
int newPosition = position % imageViewList.size();
Log.d(TAG, "onPageSelected: ");
llPoint.getChildAt(beforePager).setEnabled(false);
//把後一個頁面指派給前一個頁面
beforePager = newPosition;
llPoint.getChildAt(newPosition).setEnabled(true);
tvDescription.setText(imageDescriptionArrays[newPosition]);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
}
##自定義下拉框(用PopupWindow實作)
涉及知識點
Popupwindow的簡單使用
popupWindow = new PopupWindow(listView,mETContext.getWidth(),1200);
//點選popupwindow的外面 隐藏poupwindow
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(mETContext);
popupwindow内listview的點選事件處理
在layout的節點下設定 android:descendantFocusability="blocksDescendants"這個屬性
listview資料移除和重新整理
//删除集合資料,并重新整理listview資料設配器
list.remove(position);
myBaseAdapter.notifyDataSetChanged();
//如果集合中沒有資料了,關閉popupwindow
if (list.size() == 0) {
popupWindow.dismiss();
}
自定義下拉框代碼展示
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "Hsia";
private ImageButton mIBtn;
private EditText mETContext;
private ListView listView;
private List list;
private TextView et_text;
private PopupWindow popupWindow;
private MyBaseAdapter myBaseAdapter;
private boolean isopen = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
mIBtn.setOnClickListener(this);
}
private void init() {
mETContext = ((EditText) findViewById(R.id.et_context));
mIBtn = ((ImageButton) findViewById(R.id.ib_check));
}
@Override
public void onClick(View v) {
if (isopen) {
Log.d(TAG, "onClick: ");
initListView();
popupWindow = new PopupWindow(listView, mETContext.getWidth(), 1200);
//點選popupwindow的外面 隐藏poupwindow
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(mETContext);
isopen = false;
} else {
popupWindow.dismiss();
isopen = true;
}
}
private void initListView() {
listView = new ListView(getApplicationContext());
myBaseAdapter = new MyBaseAdapter();
initlvData();
listView.setAdapter(myBaseAdapter);
// listView.setBackgroundResource(R.drawable.listview_background); //指定listv的背景
// listView.setCacheColorHint(Color.BLACK);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
String context = list.get(position);
mETContext.setText(context);
popupWindow.dismiss();
}
});
}
private void initlvData() {
//把listview需要的資料放入到集合中
list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add("1000000" + i);
}
}
class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
view = View.inflate(getApplicationContext(), R.layout.list_item, null);
} else {
view = convertView;
}
ImageButton imageButton = (ImageButton) view.findViewById(R.id.ibtn);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//删除集合資料,并重新整理listview資料設配器
list.remove(position);
myBaseAdapter.notifyDataSetChanged();
//如果集合中沒有資料了,關閉popupwindow
if (list.size() == 0) {
popupWindow.dismiss();
}
}
});
et_text = (TextView) view.findViewById(R.id.et);
et_text.setText(list.get(position));
return view;
}
}
}