main布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.animee.forthmonth.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/main_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextColor="#FFF" />
<android.support.v4.view.ViewPager
android:id="@+id/main_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
fragment_animation:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:id="@+id/anime_iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_alpha"
android:text="透明度"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_scale"
android:text="放缩"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_tran"
android:text="平移"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_rotate"
android:text="旋转"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
fragment_View:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.animee.forthmonth.custom.ViewFragment">
<!-- TODO: Update blank fragment layout -->
<com.animee.forthmonth.custom.TitleView
android:id="@+id/title1"
android:layout_width="match_parent"
android:layout_height="60dp"
app:bgColor="#009900"
app:titleText="标题1"></com.animee.forthmonth.custom.TitleView>
<com.animee.forthmonth.custom.TitleView
android:layout_width="match_parent"
android:layout_height="60dp"
app:titleText="标题2"
android:layout_marginTop="10dp"
app:bgColor="#009900"
app:rightIvVisiable="false"></com.animee.forthmonth.custom.TitleView>
<com.animee.forthmonth.custom.TitleView
android:layout_width="match_parent"
android:layout_height="60dp"
app:titleText="标题3"
app:bgColor="#990000"
android:layout_marginTop="10dp"
app:leftIvVisiable="false"
app:rightIvVisiable="false"
app:leftTvVisiable="true"
app:rightTvVisiable="true">
</com.animee.forthmonth.custom.TitleView>
<com.animee.forthmonth.custom.TitleView
android:layout_width="match_parent"
android:layout_height="60dp"
app:bgColor="#990000"
android:layout_marginTop="10dp"
app:titleText="标题4"
app:leftIvVisiable="false"
app:leftTvVisiable="true"
app:rightIvRes="@mipmap/mail_folder_type_add_account"></com.animee.forthmonth.custom.TitleView>
<com.animee.forthmonth.custom.TitleView
android:layout_width="match_parent"
android:layout_height="60dp"
app:bgColor="#000099"
android:layout_marginTop="10dp"
app:titleText="标题5"
app:leftIvVisiable="false"
app:leftTvVisiable="true"
app:leftTextColor="#FF0000"
app:rightIvRes="@mipmap/mail_folder_type_add_account">
</com.animee.forthmonth.custom.TitleView>
<com.animee.forthmonth.custom.TitleView
android:layout_width="match_parent"
android:layout_height="60dp"
app:bgColor="#000099"
android:layout_marginTop="10dp"
app:titleText="标题6"
app:titleTextColor="#000"
app:leftIvVisiable="false"
app:leftTvVisiable="true"
app:leftTextColor="#FF0000"
app:rightIvRes="@mipmap/mail_folder_type_draft"></com.animee.forthmonth.custom.TitleView>
</LinearLayout>
title_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/title_layout"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/center_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题一"
android:textSize="22sp"
android:textColor="#FFFFFF"
android:layout_centerInParent="true"/>
<ImageView
android:id="@+id/left_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon_title_bar_filter_focus"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/left_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左边"
android:textColor="#FFF"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textSize="18sp"/>
<ImageView
android:id="@+id/right_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon_title_bar_filter_focus"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/right_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右边"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"/>
</RelativeLayout>
AnimationFragment:
package com.animee.forthmonth.animee;
import android.animation.ObjectAnimator;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import com.animee.forthmonth.R;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* A simple {@link Fragment} subclass.
*/
public class AnimationFragment extends Fragment implements View.OnClickListener{
@BindView(R.id.btn_alpha)
Button alphaBtn;
@BindView(R.id.btn_scale)
Button scaleBtn;
@BindView(R.id.btn_rotate)
Button rotateBtn;
@BindView(R.id.btn_tran)
Button tranBtn;
@BindView(R.id.anime_iv)
ImageView iv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_animation, container, false);
ButterKnife.bind(this,view);
alphaBtn.setOnClickListener(this);
scaleBtn.setOnClickListener(this);
tranBtn.setOnClickListener(this);
rotateBtn.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_alpha:
ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 1.0f, 0.5f);
alpha.setDuration(2000);
alpha.start();
break;
case R.id.btn_scale:
ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 1.0f, 2.0f);
scaleX.setDuration(2000);
scaleX.start();
break;
case R.id.btn_tran:
ObjectAnimator translationY = ObjectAnimator.ofFloat(iv, "translationY", 0, 300);
translationY.setDuration(2000);
translationY.start();
break;
case R.id.btn_rotate:
ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 180, 0);
rotation.setDuration(2000);
rotation.start();
break;
}
}
}
TitleView:
package com.animee.forthmonth.custom;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.animee.forthmonth.R;
/**
* Created by Administrator on 2018/4/4.
*/
public class TitleView extends RelativeLayout{
RelativeLayout layout;
TextView centerTv,leftTv,rightTv;
ImageView leftIv,rightIv;
public TitleView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_layout,this);
layout = (RelativeLayout) findViewById(R.id.title_layout);
centerTv = (TextView) findViewById(R.id.center_tv);
leftTv = (TextView) findViewById(R.id.left_tv);
rightTv = (TextView) findViewById(R.id.right_tv);
leftIv = (ImageView) findViewById(R.id.left_iv);
rightIv = (ImageView) findViewById(R.id.right_iv);
// 自定义属性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
int bgColor = typedArray.getColor(R.styleable.TitleView_bgColor, Color.BLUE);
layout.setBackgroundColor(bgColor);
String title = typedArray.getString(R.styleable.TitleView_titleText);
centerTv.setText(title);
int centercolor = typedArray.getColor(R.styleable.TitleView_titleTextColor, Color.WHITE);
centerTv.setTextColor(centercolor);
boolean b = typedArray.getBoolean(R.styleable.TitleView_leftIvVisiable, true);
setLeftIvVisiable(b);
boolean b2 = typedArray.getBoolean(R.styleable.TitleView_rightIvVisiable, true);
setRightIvVisiable(b2);
boolean b3 = typedArray.getBoolean(R.styleable.TitleView_leftTvVisiable, false);
setLeftTvVisiable(b3);
boolean b4 = typedArray.getBoolean(R.styleable.TitleView_rightTvVisiable, false);
setRightTvVisiable(b4);
int leftColor = typedArray.getColor(R.styleable.TitleView_leftTextColor, Color.WHITE);
leftTv.setTextColor(leftColor);
int rightcolor = typedArray.getColor(R.styleable.TitleView_rightTextColor, Color.WHITE);
rightTv.setTextColor(rightcolor);
int leftId = typedArray.getResourceId(R.styleable.TitleView_leftIvRes, R.mipmap.icon_title_bar_filter_focus);
leftIv.setImageResource(leftId);
int rightId = typedArray.getResourceId(R.styleable.TitleView_rightIvRes, R.mipmap.icon_title_bar_filter_focus);
rightIv.setImageResource(rightId);
// 回收属性
typedArray.recycle();
}
// 设置中心标题的内容
public void setTitleText(String msg){
centerTv.setText(msg);
}
// 设置左边的内容
public void setLeftText(String msg){
leftTv.setText(msg);
}
// 设置右边的内容
public void setRightText(String msg){
rightTv.setText(msg);
}
// 设置左边的图片是否显示
public void setLeftIvVisiable(boolean b){
if (b) {
leftIv.setVisibility(VISIBLE);
}else{
leftIv.setVisibility(INVISIBLE);
}
}
// 设置右边的图片是否显示
public void setRightIvVisiable(boolean b){
if (b) {
rightIv.setVisibility(VISIBLE);
}else{
rightIv.setVisibility(INVISIBLE);
}
}
// 设置左边的文字是否显示
public void setLeftTvVisiable(boolean b){
if (b) {
leftTv.setVisibility(VISIBLE);
}else{
leftTv.setVisibility(INVISIBLE);
}
}
// 设置右边的文字是否显示
public void setRightTvVisiable(boolean b){
if (b) {
rightTv.setVisibility(VISIBLE);
}else{
rightTv.setVisibility(INVISIBLE);
}
}
// 设置布局的颜色
public void setBgColor(int color){
layout.setBackgroundColor(color);
}
}
ViewFragment:
package com.animee.forthmonth.custom;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.animee.forthmonth.R;
/**
* A simple {@link Fragment} subclass.
*/
public class ViewFragment extends Fragment {
public ViewFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_view, container, false);
}
}
MyPagerAdapter:
package com.animee.forthmonth;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by Administrator on 2018/4/4.
*/
public class MyPagerAdapter extends FragmentPagerAdapter{
private List<String>titleList;
private List<Fragment>fragmentList;
public MyPagerAdapter(FragmentManager fm, List<String> titleList, List<Fragment> fragmentList) {
super(fm);
this.titleList = titleList;
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
main:
package com.animee.forthmonth;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.animee.forthmonth.animee.AnimationFragment;
import com.animee.forthmonth.custom.ViewFragment;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.main_tab)
TabLayout tabLayout;
@BindView(R.id.main_vp)
ViewPager viewPager;
List<String>titleList = new ArrayList<>();
List<Fragment>fragmentList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initPager();
}
/**
* 初始化ViewPager页面
* */
private void initPager() {
titleList.add("动画");
titleList.add("自定义控件");
titleList.add("网络数据");
fragmentList.add(new AnimationFragment());
fragmentList.add(new ViewFragment());
fragmentList.add(new AnimationFragment());
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), titleList, fragmentList);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
}