31.通过设置背景图像创建立体的质感按钮
搜索质感按钮,随便找一个
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
我这个没找好,应该把按钮外面的图层去掉才行,但是就这样了,也懒得做一个了。
<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>
image.png
参考链接:
ps如何做出质感按钮https://jingyan.baidu.com/article/925f8cb8dfbf11c0dde05691.html
32.使用FloatingActionButton创建悬浮按钮
百度找一个添加图标。
<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>
结果如下所示
image.png
参考链接:
FloatingActionButton(悬浮按钮)
https://www.jianshu.com/p/f2a4df406948
33.以全屏效果显示在ImageView中的图像
百度随便找个图片。
flower
<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>
1.jpg
这里有个知识点是PorterDuff.Mode.SRC_IN的效果,另Canvas canvas = new Canvas(newBitmap),后续canvas做的操作,会保存到newBitmap中。
35.使用单指滑动拖曳ImageView的图像
找张图。
test.png
<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;//用于判断当前手势是否为拖拽操作
private static final int DRAG = 1;//拖拽操作标志
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){//拖拽发生
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
https://www.jianshu.com/p/cdbf200122ae
Android自定义图片拖拽控件
https://www.jianshu.com/p/dbc0c94434c1
Android ImageView 的scaleType 属性图解
https://www.jianshu.com/p/32e335d5b842
36.使用Gallery实现滑动浏览多幅图像
由于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 使用示例
https://www.jianshu.com/p/38c852edc091
39.使用两幅图像定制ToggleButton开关状态
搜索,switch button、开关等关键字,找图标。最后决定用这两个。
https://www.easyicon.net/iconsearch/%E5%BC%80%E5%85%B3/?s=addtime_DESC
off.png on.png
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="已关闭"
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("已关闭背景图");
mLinearLayout.setBackground(null);
}
}
});
40.使用GridView创建网格显示多幅图像
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实现缩放轮播多幅图像
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);
}
//第一个圆点为白点表示当前显示的第一幅图像
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实现图片轮播
https://www.jianshu.com/p/c083aa9ddd83
42.使用Handler实现自动轮播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实现苹果风格的coverflow
由于coverflow现在很少使用,因此跳过。
参考链接:
在唱片店消失在大街小巷的同时,我们也失去了 Cover Flow
44.使用RecyclerView创建水平瀑布流图像
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;
//使用随机数设置宽度
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>
参考链接:
浅谈瀑布流
https://www.jianshu.com/p/cea62b6868ce
RecyclerView瀑布流的那些坑
https://www.jianshu.com/p/4e142909b824
使用RecyclerView实现瀑布流
https://www.jianshu.com/p/02be426fda0a
45.以网格或列表显示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="网格显示"
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仿表情包插入输入框
qq_1.jpg qq_2.jpg qq_3.jpg qq_4.jpg
<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="发送内容:"
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实现仿微信聊天界面(文字、表情、图片、语音、视频)
https://www.jianshu.com/p/e43142909f09
47.使用CardView显示RecyclerView列表项
参考链接
详解Android:利用RecyclerView及CardView来创建卡片列表
https://www.jianshu.com/p/582e88ee6aad
48.在ListView中创建图文结合列表项
参考链接:
Android ListView与RecyclerView对比浅析
https://www.jianshu.com/p/4d6f8b67905a
Android ListView 和 RecyclerView 详解
https://www.jianshu.com/p/da23fe946ed1
说说 Android UI 中的 ListView(列表控件)
https://www.jianshu.com/p/5df7c7d48c2c
49.使用ListPopupWindow实现下拉选择
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);//设置ListPopupWindow的锚点,即关联PopupWindow的显示位置和这个锚点
mListPopupWindow.setModal(true);//设置是否是模式
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使用
https://www.jianshu.com/p/b758cbdd49f3
50.使用Elevation创建阴影扩散的控件
android:background是要加的,否则阴影无法显现出来。
<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="设置扩散阴影"
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设置阴影的三种方式
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 控件点击水波纹效果的几种方案
https://www.jianshu.com/p/942600dc1592?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
Android水波纹点击效果
https://www.jianshu.com/p/b8101b96246a
Android 水波纹效果的探究
https://www.jianshu.com/p/13eb4574e988
52.使用自定义形状定制Switch开关状态
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开关按钮的样式
https://www.jianshu.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实现抽屉式滑动
由于SlidingDrawer废弃,跳过。
56.自定义ScrollView实现下拉回弹动画
参考链接:
实现一个带下拉弹簧动画的 ScrollView
https://www.jianshu.com/p/ce6497cada9c
57.使用CollapsingToolbarLayout实现滚动折叠
参考链接:
实现折叠式Toolbar:CollapsingToolbarLayout 使用完全解析
https://www.jianshu.com/p/8ce727308f29
58.使用BottomNavigationView实现底部导航
参考链接:
BottomNavigationView + Fragment 底部导航栏实现
https://www.jianshu.com/p/5f5dea7b5ea1
BottomNavigationView之底部导航栏的实现
https://www.jianshu.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使用详解
https://www.jianshu.com/p/f613571addb5
安卓的ProgressBar.setSecondaryProgress(int)
https://zhidao.baidu.com/question/135312516034157205.html
[译]使用Android Theme属性进行个性化
https://www.jianshu.com/p/89166488757b
60.使用ViewOutlineProvider创建圆角控件
<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="设置圆角效果"
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创建自定义时钟
由于AnalogClock废弃,跳过。
image.png
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小时制的时间显示,所以只有设置android:format24Hour才能显示全年月日等信息,如果设置android:format12Hour就只能显示小时和分钟的时间了。
参考链接:
Android 实时显示时间(textview)
https://www.jianshu.com/p/a8a37d3d9a03
TextClock使用,注意显示错误
https://blog.csdn.net/you943047219/article/details/80826855
63.使用RatingBar实现星级评分
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实现手动校验
下个图标。
https://www.easyicon.net/1182534-arrows_right_double_icon.html
使用这个链接里面的图标。
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 滑动验证效果
https://www.jianshu.com/p/f62c4da189e3
seekBar滑块验证解锁
https://blog.csdn.net/hailin123123/article/details/53260553
用安卓原生控件SeekBar实现拖动验证
https://blog.csdn.net/nannan1989yue/article/details/81570876
网友评论