Android實現新聞專案
阿新 • • 發佈:2018-11-04
這是一個新聞類的專案,由於工作原因只能是一點一點寫,後續會補充完整,有疑惑可以私信我.
首先是首頁面,歡迎頁實現 旋轉 縮放 漸變 的效果 ,三個引導頁實現側滑的同時底部的小點跟隨側滑移動.
GuideActivity.java
package com.example.lenovo.myx2_d1; import android.app.Activity; import android.content.Context; import android.media.Image; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import java.util.ArrayList; import java.util.List; /** * Created by LENOVO on 2018/08/01. */ public class GuideActivity extends Activity { private LinearLayout dotLayout; private ImageView redDot; private ViewPager viewPager; private int[] imgs = {R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3}; private int leftMargin =0 ; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.guide_activity); leftMargin = dip2px(this,10f); initView(); initLoad(); initViewPager(); } private List<ImageView> imgViews = new ArrayList<>(); private void initViewPager(){ // 把圖片int集合遍歷,同時建立多個ImageView,且設定圖片,然後把imgView放入到imgViews集合裡 for (int img :imgs){ ImageView imgView = new ImageView(this); imgView.setBackgroundResource(img); imgViews.add(imgView); } MyPager myPager = new MyPager(imgViews); viewPager.setAdapter(myPager); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { int tempMargin = leftMargin; int leftMar = (int) ((position*tempMargin)+(tempMargin*positionOffset)); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) redDot.getLayoutParams(); params.leftMargin = leftMar*2; redDot.setLayoutParams(params); } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); } private void initView() { dotLayout = findViewById(R.id.dot_layout); redDot = findViewById(R.id.red_dot); viewPager = findViewById(R.id.view_pager); } private void initLoad() { // 新增三個點到Layout for (int x = 0; x < imgs.length; x++) { ImageView dot = new ImageView(this); dot.setBackgroundResource(R.drawable.point_normal); if (x != 0) { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.leftMargin = leftMargin; dot.setLayoutParams(params); } dotLayout.addView(dot); } } /** * 根據手機的解析度從 dp 的單位 轉成為 px(畫素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } }
MyPager.java
package com.example.lenovo.myx2_d1; /** * Created by LENOVO on 2018/08/01. */ import android.support.annotation.NonNull; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class MyPager extends PagerAdapter { private List<ImageView> imgList; public MyPager(List<ImageView> imgs){ if (imgList==null){ imgList = new ArrayList<>(); imgList.addAll(imgs); } } @Override public int getCount() { return imgList.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view==object; } @NonNull // 新增子view==imageview,到container @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { ImageView img= imgList.get(position); container.addView(img); return img; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView(imgList.get(position)); } }
SplashActivity.java
package com.example.lenovo.myx2_d1; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.widget.RelativeLayout; /** * Created by LENOVO on 2018/08/01. */ public class SplashActivity extends AppCompatActivity { private RelativeLayout rootLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); // 實現 旋轉 縮放 漸變 的效果 AlphaAnimation alphaAnimation = new AlphaAnimation(0,1); alphaAnimation.setFillAfter(true); ScaleAnimation scaleAnimation=new ScaleAnimation(0,1,0,1,ScaleAnimation.RELATIVE_TO_SELF,0.5f,ScaleAnimation.RELATIVE_TO_SELF,0.5f); scaleAnimation.setFillAfter(true); RotateAnimation rotateAnimation=new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setFillAfter(true); AnimationSet animationSet = new AnimationSet(false); animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(scaleAnimation); animationSet.addAnimation(rotateAnimation); animationSet.setDuration(2000); rootLayout.startAnimation(animationSet); animationSet.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { Intent intent = new Intent(SplashActivity.this,GuideActivity.class); startActivity(intent); finish(); } @Override public void onAnimationRepeat(Animation animation) { } }); } private void initView(){ rootLayout = findViewById(R.id.root_layout); } private void initLoad(){ } }
以下是佈局檔案
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_bg_newyear">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/splash_sheep_newyear" />
</RelativeLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".SplashActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
guide_activity.xml
<?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="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/dot_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<ImageView
android:id="@+id/red_dot"
android:background="@drawable/point_red"
android:layout_width="10dp"
android:layout_height="10dp" />
</RelativeLayout>
</RelativeLayout>
values/style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.myx2_d1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GuideActivity"></activity>
</application>
</manifest>