31.通過設(shè)置背景圖像創(chuàng)建立體的質(zhì)感按鈕
搜索質(zhì)感按鈕电湘,隨便找一個
http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E8%B4%A8%E6%84%9F%E5%AF%BC%E8%88%AA%E6%8C%89%E9%92%AE
我這個沒找好购笆,應(yīng)該把按鈕外面的圖層去掉才行蒜魄,但是就這樣了,也懶得做一個了食侮。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_margin="5dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textSize="20dp"
android:text="AAAAAA"
android:textColor="#fff"/>
<Button
android:layout_margin="5dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textSize="20dp"
android:text="BBBBBB"
android:textColor="#fff"/>
</LinearLayout>
參考鏈接:
ps如何做出質(zhì)感按鈕https://jingyan.baidu.com/article/925f8cb8dfbf11c0dde05691.html
32.使用FloatingActionButton創(chuàng)建懸浮按鈕
百度找一個添加圖標(biāo)昂儒。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src = "@mipmap/add"
app:backgroundTint="#FFF"
app:fabSize = "normal"/>
</LinearLayout>
結(jié)果如下所示
參考鏈接:
FloatingActionButton(懸浮按鈕)
http://www.reibang.com/p/f2a4df406948
33.以全屏效果顯示在ImageView中的圖像
百度隨便找個圖片。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src = "@mipmap/flower"
android:scaleType="fitXY"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
}
manifest中 android:theme改成如下所示
android:theme="@style/Theme.AppCompat.NoActionBar"
34.在自定義ImageView中顯示圓形圖像
public class CircleImageView extends android.support.v7.widget.AppCompatImageView{
public CircleImageView(Context context) {
super(context);
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleImageView(Context context, AttributeSet attrs,int defStyle) {
super(context, attrs,defStyle);
}
@Override
protected void onDraw(Canvas canvas){
Drawable drawable = getDrawable();
if (drawable == null){
return;
}
if (getWidth() == 0 || getHeight() == 0){
return;
}
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap oldBitmap = bitmap.copy(Bitmap.Config.ARGB_8888,true);
int radius = getWidth();
Bitmap circleBmp = getCroppedBitmap(oldBitmap,radius);
canvas.drawBitmap(circleBmp,0,0,null);
}
public static Bitmap getCroppedBitmap(Bitmap oldBitmap,int radius){
Bitmap bitmap;
if (oldBitmap.getWidth() != radius || oldBitmap.getHeight() != radius){
bitmap = Bitmap.createScaledBitmap(oldBitmap,radius,radius,false);
}else {
bitmap = oldBitmap;
}
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
final Paint paint = new Paint();
final Rect rect = new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawCircle(bitmap.getWidth()/2,bitmap.getHeight()/2,
bitmap.getWidth()/2,paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap,rect,rect,paint);
return newBitmap;
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<com.cc.uisample.CircleImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src = "@mipmap/flower"/>
</LinearLayout>
這里有個知識點是PorterDuff.Mode.SRC_IN的效果愚争,另Canvas canvas = new Canvas(newBitmap),后續(xù)canvas做的操作挤聘,會保存到newBitmap中轰枝。
35.使用單指滑動拖曳ImageView的圖像
找張圖。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/test"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private PointF mStartPoint = new PointF();
private Matrix mMatrix = new Matrix();
private Matrix mCurrentMatrix = new Matrix();
private int MODE = 0;//用于判斷當(dāng)前手勢是否為拖拽操作
private static final int DRAG = 1;//拖拽操作標(biāo)志
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView)findViewById(R.id.image_view);
mImageView.setScaleType(ImageView.ScaleType.MATRIX);
mImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
MODE = DRAG;
mCurrentMatrix.set(mImageView.getImageMatrix());
mStartPoint.set(motionEvent.getX(),motionEvent.getY());//記錄起始位置
break;
case MotionEvent.ACTION_MOVE:
if (MODE == DRAG){//拖拽發(fā)生
float dx = motionEvent.getX() - mStartPoint.x ;//x軸移動距離
float dy = motionEvent.getY() - mStartPoint.y;//y軸移動距離
mMatrix.set(mCurrentMatrix);
mMatrix.postTranslate(dx,dy);
}
break;
case MotionEvent.ACTION_UP:
MODE = 0;
break;
case MotionEvent.ACTION_POINTER_UP:
MODE = 0;
break;
}
mImageView.setImageMatrix(mMatrix);
return true;
}
});
}
}
參考鏈接:
Android跟隨手指移動的view
http://www.reibang.com/p/cdbf200122ae
Android自定義圖片拖拽控件
http://www.reibang.com/p/dbc0c94434c1
Android ImageView 的scaleType 屬性圖解
http://www.reibang.com/p/32e335d5b842
36.使用Gallery實現(xiàn)滑動瀏覽多幅圖像
由于Gallery廢棄组去,跳過鞍陨。
37.使用SwipeRefreshLayout切換圖像
SwipeRefreshLayout是下拉刷新控件。項目運行后,下拉圖片即顯示诚撵。
public class MainActivity extends AppCompatActivity {
private ImageView mImageView;
private SwipeRefreshLayout mSwipeRefreshLayout;
private Boolean bChecked = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
mImageView = (ImageView)findViewById(R.id.image_view);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
if (bChecked){
mImageView.setImageDrawable(getDrawable(R.mipmap.image1));
}else {
mImageView.setImageDrawable(getDrawable(R.mipmap.image2));
}
bChecked = !bChecked;
}
},1000);
}
});
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
38.使用AdapterViewFlipper自動播放圖像
public class MainActivity extends AppCompatActivity {
private int[] images = new int[]{
R.mipmap.image1,R.mipmap.image2,R.mipmap.image3
};
private BaseAdapter mBaseAdapter;
private AdapterViewFlipper mAdapterViewFlipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBaseAdapter = new BaseAdapter() {
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(images[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return imageView;
}
};
mAdapterViewFlipper = (AdapterViewFlipper) findViewById(R.id.flipper);
mAdapterViewFlipper.setAdapter(mBaseAdapter);
}
public void prev(View view) {
mAdapterViewFlipper.showPrevious();
}
public void next(View view) {
mAdapterViewFlipper.showNext();
}
public void auto(View view) {
if (mAdapterViewFlipper.isFlipping()) {
mAdapterViewFlipper.stopFlipping();
} else {
mAdapterViewFlipper.startFlipping();
}
}
}
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AdapterViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="2000"
android:layout_alignParentTop="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:onClick="prev"
android:text="上一幅圖像"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="next"
android:text="下一幅圖像"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:onClick="auto"
android:text="自動播放"
/>
</RelativeLayout>
參考鏈接:
Android AdapterViewFlipper 使用示例
http://www.reibang.com/p/38c852edc091
39.使用兩幅圖像定制ToggleButton開關(guān)狀態(tài)
搜索缭裆,switch button、開關(guān)等關(guān)鍵字寿烟,找圖標(biāo)澈驼。最后決定用這兩個。
https://www.easyicon.net/iconsearch/%E5%BC%80%E5%85%B3/?s=addtime_DESC
switch_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/on"
android:state_checked="true"/>
<item android:drawable="@drawable/off"
android:state_checked="false"/>
</selector>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已關(guān)閉"
android:textSize="40sp"/>
<ToggleButton
android:id="@+id/toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/switch_button"
android:checked="false"
android:text=""
android:textOff=""
android:textOn=""/>
</LinearLayout>
getDrawable(int)廢棄筛武。
MainActivity
public class MainActivity extends AppCompatActivity {
private LinearLayout mLinearLayout;
private TextView mTextView;
private ToggleButton mToggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayout = (LinearLayout) findViewById(R.id.linear_layout);
mTextView = (TextView) findViewById(R.id.text_view);
mToggleButton = (ToggleButton) findViewById(R.id.toggle_button);
mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
mTextView.setText("已開啟背景圖");
mLinearLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.test, null));
}else {
mTextView.setText("已關(guān)閉背景圖");
mLinearLayout.setBackground(null);
}
}
});
40.使用GridView創(chuàng)建網(wǎng)格顯示多幅圖像
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="6dp"
android:numColumns="auto_fit"
android:paddingTop="6dp"
android:stretchMode="columnWidth"
android:verticalSpacing="6dp"/>
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private GridView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView) findViewById(R.id.grid_view);
mGridView.setAdapter(new ImageAdapter(this));
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
i = i + 1;
Toast.makeText(MainActivity.this,"你點擊了第" + i + "幅圖像",Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter{
private Context mContext;
private int[] mImageBox = {R.drawable.add,R.drawable.button,R.drawable.led,R.drawable.test,
R.drawable.add,R.drawable.button,R.drawable.led,R.drawable.test,
R.drawable.add,R.drawable.button,R.drawable.led,R.drawable.test,
R.drawable.add,R.drawable.button,R.drawable.led,R.drawable.test,
R.drawable.add,R.drawable.button,R.drawable.led,R.drawable.test};
public ImageAdapter(Context context){
mContext = context;
}
@Override
public int getCount() {
return mImageBox.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageView;
if (view == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(260,260));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,0,0,0);
}else {
imageView = (ImageView) view;
}
imageView.setImageResource(mImageBox[i]);
return imageView;
}
}
}
41.使用ViewPager實現(xiàn)縮放輪播多幅圖像
public class MyAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener{
public int mPosition = 0;
public int mWidthPadding;
public int mHeightPadding;
public List<View> mViewList = new ArrayList<>();
public Context mContext;
public LinearLayout mLinearLayout;
public Integer[] mImages = {R.mipmap.image1,R.mipmap.image2,R.mipmap.image3};
public MyAdapter(Context context,LinearLayout linearLayout){
mContext = context;
initView();
mLinearLayout = linearLayout;
//進場和退場縮放圖像的寬度和高度
mWidthPadding = 100;
mHeightPadding = 150;
}
private void initView(){
for (int i = 0;i<3;i++){
ImageView imageView = new ImageView(mContext);
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),mImages[i]);
imageView.setImageBitmap(bitmap);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
mViewList.add(imageView);
}
}
@Override
public int getCount() {
return mViewList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
for (int i = 0;i<mViewList.size();i++){
mLinearLayout.getChildAt(i).setBackgroundResource(R.drawable.dot_unselected);
}
if (position<mViewList.size()){
mPosition = position;
mLinearLayout.getChildAt(position).setBackgroundResource(R.drawable.dot_selected);
//退場縮小圖像
int outHeightPadding = (int) (positionOffset * mHeightPadding);
int outWidthPadding = (int) (positionOffset * mWidthPadding);
mViewList.get(position).setPadding(outWidthPadding,outHeightPadding,outWidthPadding,outHeightPadding);
//進場放大圖像
if (position<mViewList.size()-1){
int inHeightPadding = (int) ((1 - positionOffset) * mHeightPadding);
int inWidthPadding = (int) ((1 - positionOffset) * mWidthPadding);
mViewList.get(position + 1).setPadding(inWidthPadding,inHeightPadding,inWidthPadding,inHeightPadding);
}
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mViewList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = mViewList.get(position);
container.addView(view);
return view;
}
}
MainActivity
public class MainActivity extends AppCompatActivity{
private ViewPager mViewPager;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mLinearLayout = (LinearLayout) findViewById(R.id.linear_layout);
for (int i = 0;i<3;i++){
ImageView dot = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
dot.setImageResource(R.drawable.dot_unselected);
mLinearLayout.addView(dot);
}
//第一個圓點為白點表示當(dāng)前顯示的第一幅圖像
mLinearLayout.getChildAt(0).setBackgroundResource(R.drawable.dot_selected);
MyAdapter myAdapter = new MyAdapter(this,mLinearLayout);
mViewPager.setAdapter(myAdapter);
mViewPager.addOnPageChangeListener(myAdapter);
}
}
dot_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="15dp"
android:height="15dp"/>
<solid android:color="#FFF"/>
</shape>
dot_unselected
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="15dp"
android:height="15dp"/>
<solid android:color="#44FF0000"/>
</shape>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toTopOf="@+id/linear_layout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.0"/>
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
參考鏈接:
使用ViewPager實現(xiàn)圖片輪播
http://www.reibang.com/p/c083aa9ddd83
42.使用Handler實現(xiàn)自動輪播ViewPager
public class MainActivity extends AppCompatActivity{
private ViewPager mViewPager;
private ViewPagerAdapter mViewPagerAdapter;
private int[] mImages;
public Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mImages = new int[]{R.mipmap.image1,R.mipmap.image2,R.mipmap.image3};
mViewPagerAdapter = new ViewPagerAdapter(this,mImages);
mViewPager.setAdapter(mViewPagerAdapter);
mViewPager.setOffscreenPageLimit(mImages.length);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mViewPager.setCurrentItem((mViewPager.getCurrentItem() + 1) % mViewPager.getChildCount());
mHandler.postDelayed(this,1500);
}
},1500);
}
public class ViewPagerAdapter extends PagerAdapter{
private Context mContext;
private int[] mImages;
public ViewPagerAdapter(Context context, int[] images){
mContext = context;
mImages = images;
}
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.addView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = createImageView(mContext,position);
container.addView(imageView);
return imageView;
}
private ImageView createImageView(Context context,int position){
ImageView imageView = new ImageView(context);
ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
imageView.setLayoutParams(layoutParams);
imageView.setImageResource(mImages[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imageView;
}
}
}
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="495dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"/>
</android.support.constraint.ConstraintLayout>
43.使用ViewPager實現(xiàn)蘋果風(fēng)格的coverflow
由于coverflow現(xiàn)在很少使用缝其,因此跳過。
參考鏈接:
在唱片店消失在大街小巷的同時徘六,我們也失去了 Cover Flow
44.使用RecyclerView創(chuàng)建水平瀑布流圖像
public class MainActivity extends AppCompatActivity{
private RecyclerView mRecyclerView;
private List<Item> mItemList = new ArrayList<>();
private int[] mImages = new int[]{R.mipmap.image1,R.mipmap.image2,R.mipmap.image3,R.mipmap.image1,R.mipmap.image2,R.mipmap.image3,R.mipmap.image1,R.mipmap.image2,R.mipmap.image3};;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 100;i++){
Item item = new Item();
int min = 0;
int max = 8;
Random random = new Random();
int num = random.nextInt(max) % (max - min + 1) + min;
item.setId(mImages[num]);
mItemList.add(item);
}
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
ItemAdapter itemAdapter = new ItemAdapter(mItemList);
mRecyclerView.setAdapter(itemAdapter);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager( new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL));
}
private class ItemHolder extends RecyclerView.ViewHolder{
private ImageView mImageView;
public ItemHolder(View itemView) {
super(itemView);
int min = 50;
int max = 350;
Random random = new Random();
int num = random.nextInt(max) % (max - min) + min;
//使用隨機數(shù)設(shè)置寬度
mImageView = (ImageView) itemView.findViewById(R.id.image_view);
ViewGroup.LayoutParams layoutParams = mImageView.getLayoutParams();
layoutParams.width = num;
mImageView.setLayoutParams(layoutParams);
}
}
public class ItemAdapter extends RecyclerView.Adapter<ItemHolder>{
private List<Item> mItemList;
public ItemAdapter(List<Item> itemList){
mItemList = itemList;
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View view = layoutInflater.inflate(R.layout.item_layout,null);
return new ItemHolder(view);
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
final Item item = mItemList.get(position);
holder.mImageView.setImageResource(item.getId());
}
@Override
public int getItemCount() {
return mItemList.size();
}
}
public class Item{
private int mId;
public int getId() {
return mId;
}
public void setId(int id) {
mId = id;
}
}
}
activity_main
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="495dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"/>
</android.support.constraint.ConstraintLayout>
item_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
參考鏈接:
淺談瀑布流
http://www.reibang.com/p/cea62b6868ce
RecyclerView瀑布流的那些坑
http://www.reibang.com/p/4e142909b824
使用RecyclerView實現(xiàn)瀑布流
http://www.reibang.com/p/02be426fda0a
45.以網(wǎng)格或列表顯示RecyclerView列表項
linear_item
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.096"/>
<TextView
android:id="@+id/text_view"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/image_view"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.096"/>
</android.support.constraint.ConstraintLayout>
grid_item
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="150dp"
android:layout_height="120dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/text_view"
android:layout_width="114dp"
android:layout_height="42dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image_view"/>
</android.support.constraint.ConstraintLayout>
activity_main
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="334dp"
android:layout_height="398dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="32dp"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網(wǎng)格顯示"
android:layout_marginLeft="60dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="列表顯示"
android:layout_marginRight="60dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"/>
</android.support.constraint.ConstraintLayout>
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
public boolean bGrid = false;
public Context mContext;
public String[] mStrings = {"A","B","C","D"};
public Integer[] mImages = {R.mipmap.image1,R.mipmap.image2,R.mipmap.image3,R.mipmap.flower};
public MyAdapter(Context context,boolean isGrid){
mContext = context;
bGrid = isGrid;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder myViewHolder = null;
if (bGrid){
myViewHolder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.grid_item,parent,false));
}else {
myViewHolder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.linear_item,parent,false));
}
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mTextView.setText(mStrings[position]);
holder.mImageView.setImageResource(mImages[position]);
}
@Override
public int getItemCount() {
return mStrings.length;
}
class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView mImageView;
private TextView mTextView;
public MyViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.image_view);
mTextView = (TextView) itemView.findViewById(R.id.text_view);
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity{
private RecyclerView mRecyclerView;
private Button mGridButton;
private Button mListButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
mRecyclerView.setAdapter(new MyAdapter(this,true));
mGridButton = (Button) findViewById(R.id.button);
mGridButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
mRecyclerView.setAdapter(new MyAdapter(MainActivity.this,true));
}
});
mListButton = (Button) findViewById(R.id.button2);
mListButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mRecyclerView.setAdapter(new MyAdapter(MainActivity.this,false));
}
});
}
}
46.使用RecyclerView仿表情包插入輸入框
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="245dp"
android:layout_height="229dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/textView"
android:layout_marginLeft="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintHorizontal_bias="0.166"/>
<TextView
android:id="@+id/textView"
android:layout_width="77dp"
android:layout_height="28dp"
android:text="發(fā)送內(nèi)容:"
android:layout_marginLeft="34dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="76dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="316dp"
android:layout_height="198dp"
android:layout_marginBottom="32dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.509"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
item.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener{
public Context mContext;
public Integer[] mImages = {R.drawable.qq,R.drawable.qq_1,R.drawable.qq_2,
R.drawable.qq_3 ,R.drawable.qq_4};
private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener;
@Override
public void onClick(View view) {
if (mOnRecyclerViewItemClickListener != null){
mOnRecyclerViewItemClickListener.onItemClick(view,(Integer) view.getTag());
}
}
public interface OnRecyclerViewItemClickListener{
void onItemClick(View view,Integer data);
}
public MyAdapter(Context context){
mContext = context;
}
public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
mOnRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
view.setOnClickListener(this);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mImageView.setImageResource(mImages[position]);
holder.itemView.setTag(mImages[position]);
}
@Override
public int getItemCount() {
return mImages.length;
}
class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView mImageView;
public MyViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.image_view);
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity{
private RecyclerView mRecyclerView;
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = (EditText) findViewById(R.id.edit_text);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
MyAdapter myAdapter = new MyAdapter(this);
myAdapter.setOnRecyclerViewItemClickListener(new MyAdapter.OnRecyclerViewItemClickListener() {
@Override
public void onItemClick(View view, Integer data) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),data);
ImageSpan imageSpan = new ImageSpan(MainActivity.this,bitmap);
SpannableString spannableString = new SpannableString("face");
spannableString.setSpan(imageSpan,0,4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mEditText.append(spannableString);
}
});
mRecyclerView.setAdapter(myAdapter);
}
}
參考鏈接:
Android中使用RecyclerView實現(xiàn)仿微信聊天界面(文字内边、表情、圖片待锈、語音漠其、視頻)
http://www.reibang.com/p/e43142909f09
47.使用CardView顯示RecyclerView列表項
參考鏈接
詳解Android:利用RecyclerView及CardView來創(chuàng)建卡片列表
http://www.reibang.com/p/582e88ee6aad
48.在ListView中創(chuàng)建圖文結(jié)合列表項
參考鏈接:
Android ListView與RecyclerView對比淺析
http://www.reibang.com/p/4d6f8b67905a
Android ListView 和 RecyclerView 詳解
http://www.reibang.com/p/da23fe946ed1
說說 Android UI 中的 ListView(列表控件)
http://www.reibang.com/p/5df7c7d48c2c
49.使用ListPopupWindow實現(xiàn)下拉選擇
public class MainActivity extends AppCompatActivity{
private EditText mEditText;
private ListPopupWindow mListPopupWindow;
private List<String> mStringList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = (EditText) findViewById(R.id.edit_text);
mStringList.add("A");
mStringList.add("B");
mStringList.add("C");
mStringList.add("D");
mStringList.add("E");
mStringList.add("F");
mListPopupWindow = new ListPopupWindow(this);
mListPopupWindow.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,mStringList));
mListPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mListPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mListPopupWindow.setAnchorView(mEditText);//設(shè)置ListPopupWindow的錨點,即關(guān)聯(lián)PopupWindow的顯示位置和這個錨點
mListPopupWindow.setModal(true);//設(shè)置是否是模式
mListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mEditText.setText(mStringList.get(position));
mListPopupWindow.dismiss();
}
});
mEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListPopupWindow.show();
}
});
}
}
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
參考鏈接:
ListPopupWindow使用
http://www.reibang.com/p/b758cbdd49f3
50.使用Elevation創(chuàng)建陰影擴散的控件
android:background是要加的竿音,否則陰影無法顯現(xiàn)出來和屎。
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="設(shè)置擴散陰影"
android:layout_marginLeft="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="32dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移除擴散陰影"
android:layout_marginRight="32dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="32dp"/>
<ImageView
android:id="@+id/imageView"
android:background="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/test"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="120dp"
app:layout_constraintHorizontal_bias="0.5"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity{
private Button mButton1;
private Button mButton2;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView);
mButton1 = (Button) findViewById(R.id.button1);
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mImageView.setElevation(35);
}
});
mButton2 = (Button) findViewById(R.id.button2);
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mImageView.setElevation(0);
}
});
}
}
參考鏈接:
Android中給View設(shè)置陰影的三種方式
https://blog.csdn.net/wang29169/article/details/84206379
51.在單擊CheckBox時顯示波紋擴散效果
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="100dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:layout_marginTop="56dp"
app:layout_constraintTop_toBottomOf="@+id/checkBox"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:background="?attr/selectableItemBackground"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:layout_marginTop="54dp"
app:layout_constraintTop_toBottomOf="@+id/checkBox2"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:background="?attr/selectableItemBackgroundBorderless"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
?attr/selectableItemBackground和?attr/selectableItemBackgroundBorderless是android 自帶的,效果還可以自己寫谍失。
參考鏈接:
android 控件點擊水波紋效果的幾種方案
http://www.reibang.com/p/942600dc1592?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
Android水波紋點擊效果
http://www.reibang.com/p/b8101b96246a
Android 水波紋效果的探究
http://www.reibang.com/p/13eb4574e988
52.使用自定義形狀定制Switch開關(guān)狀態(tài)
switch_thumb
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="30dp"
android:height="30dp"/>
<solid android:color="#FFF"/>
</shape>
switch_on
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#B6D6FE"/>
<corners android:radius="30dp" />
</shape>
switch_off
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#E3E3E3"/>
<corners android:radius="30dp" />
</shape>
switch_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_on" android:state_checked="true" />
<item android:drawable="@drawable/switch_off" android:state_checked="false" />
</selector>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="@+id/switch_view"
android:layout_width="98dp"
android:layout_height="57dp"
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_selector"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="100dp"
app:layout_constraintHorizontal_bias="0.533"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
參考鏈接:
Android 自定義Switch開關(guān)按鈕的樣式
http://www.reibang.com/p/4e436300f328
53.自定義selector以漸變前景切換控件
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="150dp"
android:clickable="true"
android:foreground="@drawable/selector"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="155dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ASDRHTHJTYJDGBRTRTHDFEGARGAERGEARGEARFWAAWFRWEFRWEFJRJHRSJSSRTHRSH"
android:textSize="20sp"/>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient android:startColor="#AA0000FF"
android:endColor="#aa00ff00"
android:type="linear"/>
</shape>
</item>
<item>
<shape>
<solid android:color="@android:color/transparent"/>
</shape>
</item>
</selector>
MainActivity
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
54.使用ViewSwitcher平滑切換兩個View
public class MainActivity extends AppCompatActivity{
private ViewSwitcher mViewSwitcher;
private Animation mAnimationLeft;
private Animation mAnimationRight;
private Button mButton1;
private Button mButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewSwitcher = (ViewSwitcher) findViewById(R.id.view_switcher);
mButton1 = (Button) findViewById(R.id.button);
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mViewSwitcher.showPrevious();
}
});
mButton2 = (Button) findViewById(R.id.button2);
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mViewSwitcher.showNext();
}
});
mAnimationLeft = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
mAnimationRight = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
mViewSwitcher.setInAnimation(mAnimationLeft);
mViewSwitcher.setInAnimation(mAnimationRight);
}
}
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewSwitcher
android:id="@+id/view_switcher"
android:layout_width="282dp"
android:layout_height="395dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src = "@mipmap/image2"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/image1"/>
</ViewSwitcher>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一個視圖"
android:layout_marginLeft="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一個視圖"
android:layout_marginRight="32dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"/>
</android.support.constraint.ConstraintLayout>
參考鏈接:
Android ViewSwitcher簡介和使用
https://blog.csdn.net/zhangphil/article/details/48312811
ViewSwitcher類解析眶俩,可以用來在兩個View中切換顯示莹汤,并添加切換動畫
https://blog.csdn.net/zyRocky1993/article/details/60321519
55.使用SlidingDrawer實現(xiàn)抽屜式滑動
由于SlidingDrawer廢棄快鱼,跳過。
56.自定義ScrollView實現(xiàn)下拉回彈動畫
參考鏈接:
實現(xiàn)一個帶下拉彈簧動畫的 ScrollView
http://www.reibang.com/p/ce6497cada9c
57.使用CollapsingToolbarLayout實現(xiàn)滾動折疊
參考鏈接:
實現(xiàn)折疊式Toolbar:CollapsingToolbarLayout 使用完全解析
http://www.reibang.com/p/8ce727308f29
58.使用BottomNavigationView實現(xiàn)底部導(dǎo)航
參考鏈接:
BottomNavigationView + Fragment 底部導(dǎo)航欄實現(xiàn)
http://www.reibang.com/p/5f5dea7b5ea1
BottomNavigationView之底部導(dǎo)航欄的實現(xiàn)
http://www.reibang.com/p/0d9171fecf69
59.在ProgressBar上同時顯示兩種進度
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:layout_width="288dp"
android:layout_height="40dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="90dp"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加進度"
android:layout_marginLeft="48dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="減少進度"
android:layout_marginRight="48dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="275dp"
android:layout_height="51dp"
android:text="TextView"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/progressBar"
app:layout_constraintHorizontal_bias="0.5"
android:textSize="20sp"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity{
private ProgressBar mProgressBar;
private TextView mTextView;
private Button mButton1;
private Button mButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mTextView = (TextView) findViewById(R.id.textView);
mProgressBar.setSecondaryProgressTintList( AppCompatResources.getColorStateList(this, android.R.color.holo_red_dark));
mTextView.setText("第一進度 " + mProgressBar.getProgress() * 100.0 / mProgressBar.getMax() + "%" + "第二進度:"
+ mProgressBar.getSecondaryProgress() * 100.0 / mProgressBar.getMax() + "%" );
mButton1 = (Button) findViewById(R.id.button1);
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mProgressBar.incrementProgressBy(10);
mProgressBar.setSecondaryProgress(mProgressBar.getProgress() + 30);
mTextView.setText("第一進度 " + mProgressBar.getProgress() * 100.0 / mProgressBar.getMax() + "%" + "第二進度:"
+ mProgressBar.getSecondaryProgress() * 100.0 / mProgressBar.getMax() + "%" );
}
});
mButton2 = (Button) findViewById(R.id.button2);
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mProgressBar.incrementProgressBy(-10);
mProgressBar.setSecondaryProgress(mProgressBar.getProgress() + 30);
mTextView.setText("第一進度 " + mProgressBar.getProgress() * 100.0 / mProgressBar.getMax() + "%" + "第二進度:"
+ mProgressBar.getSecondaryProgress() * 100.0 / mProgressBar.getMax() + "%" );
}
});
}
}
getColorStateList(int id)廢棄,使用AppCompatResources.getColorStateList()纲岭。
參考鏈接:
ProgressBar使用詳解
http://www.reibang.com/p/f613571addb5
安卓的ProgressBar.setSecondaryProgress(int)
https://zhidao.baidu.com/question/135312516034157205.html
[譯]使用Android Theme屬性進行個性化
http://www.reibang.com/p/89166488757b
60.使用ViewOutlineProvider創(chuàng)建圓角控件
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="設(shè)置圓角效果"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/button2"
android:layout_marginRight="8dp"
android:onClick="onClickBtn1"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消圓角效果"
android:layout_marginRight="56dp"
app:layout_constraintRight_toRightOf="parent"
android:onClick="onClickBtn2"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/test"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="64dp"
app:layout_constraintHorizontal_bias="0.504"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView);
ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),60);
}
};
mImageView.setOutlineProvider(viewOutlineProvider);
}
public void onClickBtn1(View view){
mImageView.setClipToOutline(true);
}
public void onClickBtn2(View view){
mImageView.setClipToOutline(false);
}
}
61.使用AnalogClock創(chuàng)建自定義時鐘
由于AnalogClock廢棄抹竹,跳過。
62.在TextClock中定制日期格式
public class MainActivity extends AppCompatActivity {
private TextClock mTextClock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextClock = (TextClock) findViewById(R.id.text_clock);
new Timer().schedule(new TimerTask() {//周期性改變時鐘
@Override
public void run() {
mTextClock.postInvalidate();
}
},0,1000);
}
}
xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextClock
android:id="@+id/text_clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#00f"
android:format24Hour="yyyy/MM/dd HH:mm:ss"
/>
<!-- yyyy年MM月dd日 a hh:mm:ss EEEE-->
</LinearLayout>
這里有個需要注意的地方止潮,由于我手機本身是24小時制的時間顯示窃判,所以只有設(shè)置android:format24Hour才能顯示全年月日等信息,如果設(shè)置android:format12Hour就只能顯示小時和分鐘的時間了喇闸。
參考鏈接:
Android 實時顯示時間(textview)
http://www.reibang.com/p/a8a37d3d9a03
TextClock使用袄琳,注意顯示錯誤
https://blog.csdn.net/you943047219/article/details/80826855
63.使用RatingBar實現(xiàn)星級評分
public class MainActivity extends AppCompatActivity {
private ImageView mImageView;
private RatingBar mRatingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image_view);
mRatingBar = (RatingBar) findViewById(R.id.rating_bar);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
//setAlpha(float) float取值范圍為0.0f-1.0f setAlpha(int)廢棄
mImageView.setAlpha( v / 5);
}
});
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/image1"/>
<RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
64.在登錄窗口中使用SeekBar實現(xiàn)手動校驗
下個圖標(biāo)。
https://www.easyicon.net/1182534-arrows_right_double_icon.html
使用這個鏈接里面的圖標(biāo)燃乍。
https://blog.csdn.net/hailin123123/article/details/53260553
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="80dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="0dp"
android:layout_marginTop="40dp"
android:text="賬戶名稱:"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="0dp"
android:layout_marginTop="100dp"
android:text="賬戶密碼:"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="@+id/edit_text2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline2"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.3"/>
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="320dp"
android:layout_height="57dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="286dp"
android:progressDrawable="@drawable/bg"
android:splitTrack="false"
android:thumb="@drawable/thumb"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請按住滑塊唆樊,拖動到最右邊"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="309dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
android:layout_marginLeft="80dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="191dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="191dp"
android:layout_marginRight="80dp"
android:text="放棄"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity{
private TextView mTextView;
private SeekBar mSeekBar;
private Button mLoginButton;
public Handler mHandler = new Handler(){
@Override
public void handleMessage(Message message){
if (message.what == 1){
mSeekBar.setVisibility(View.GONE);
mTextView.setVisibility(View.GONE);
}
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"登錄成功",Toast.LENGTH_SHORT).show();
finish();
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mSeekBar = (SeekBar) findViewById(R.id.seek_bar);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (seekBar.getProgress() == seekBar.getMax()){
mTextView.setVisibility(View.VISIBLE);
mTextView.setTextColor(Color.WHITE);
mTextView.setText("驗證成功!");
mLoginButton.setClickable(true);
new Thread(){
@Override
public void run(){
try {
Thread.sleep(1000);
mHandler.sendEmptyMessage(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}else {
mTextView.setVisibility(View.INVISIBLE);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (seekBar.getProgress() != seekBar.getMax()){
seekBar.setProgress(0);
mTextView.setVisibility(View.VISIBLE);
mTextView.setTextColor(Color.GRAY);
mTextView.setText("向右移動刻蟹,完成驗證");
}
}
});
mLoginButton = (Button) findViewById(R.id.button);
mLoginButton.setClickable(false);
}
}
參考鏈接:
SeekBar 滑動驗證效果
http://www.reibang.com/p/f62c4da189e3
seekBar滑塊驗證解鎖
https://blog.csdn.net/hailin123123/article/details/53260553
用安卓原生控件SeekBar實現(xiàn)拖動驗證
https://blog.csdn.net/nannan1989yue/article/details/81570876