Android 高级编程【6个实战案例(附源码):刮刮卡、补间动画、逐帧动画、Fragment、RecyclerView、下拉刷新】
目? ?錄
刮刮卡案例【ScratchCard】
結構圖
activity_main.xml
MainActivity.java
運行效果圖
補間動畫(Tween Animation)
逐幀動畫(Frame Animation)
Fragment
RecyclerView 控件
導入recyclerView控件包
實戰演練
activity_main.xml
ltem_home.xml
MainActivity.java
下拉刷新
activity_main.xml
MainActivity.java
刮刮卡案例【ScratchCard】
源碼【可用Gitee直接拷貝】:https://gitee.com/lwx001/ScratchCard
結構圖
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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"tools:context=".MainActivity"><ImageViewandroid:id="@+id/bg"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg" /><ImageViewandroid:id="@+id/imgv"android:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="centerCrop"android:src="@drawable/scratch_card" /> </RelativeLayout>MainActivity.java
package cn.lwx.scratchcard;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint;import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView;public class MainActivity extends AppCompatActivity {private ImageView imageView;private Bitmap alterbitmap;private double nX, nY;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView = (ImageView) findViewById(R.id.imgv);//從資源文件中解析一張bitmapBitmap bitmap =BitmapFactory.decodeResource(getResources(),R.drawable.scratch_card);alterbitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);nX = (double) bitmap.getWidth() / dm.widthPixels;nY = (double) bitmap.getHeight() / dm.heightPixels;//創建一個canvas對象Canvas canvas = new Canvas(alterbitmap);//創建畫筆對象Paint paint = new Paint();//為畫筆設置顏色paint.setColor(Color.BLACK);paint.setAntiAlias(true);//創建Matrix對象Matrix matrix = new Matrix();//在alterBitmap上畫圖canvas.drawBitmap(bitmap, matrix, paint);//為ImageView設置觸摸監聽imageView.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {try {int x = (int) event.getX();int y = (int) event.getY();for (int i = -100; i < 100; i++) {for (int j = -100; j < 100; j++) {//將區域類的像素點設為透明像素if (Math.sqrt((i * i) + (j * j)) <= 100) {alterbitmap.setPixel((int) (x * nX) + i,(int) (y * nY + 90) + j, Color.TRANSPARENT);}}}imageView.setImageBitmap(alterbitmap);} catch (Exception e) {//加try{}catch(){}放置用戶觸摸圖片以外的地方而異常退出e.printStackTrace();}//銷毀掉該觸摸事件return true;}});} }運行效果圖
補間動畫(Tween Animation)
源碼【可用Gitee直接拷貝】:https://gitee.com/lwx001/Tween?
逐幀動畫(Frame Animation)
源碼:https://gitee.com/lwx001/Frame
?
Fragment
推薦學習網址:菜鳥教程
https://www.runoob.com/w3cnote/android-tutorial-fragment-base.html
ViewPager的簡單使用
https://www.runoob.com/w3cnote/android-tutorial-viewpager.html
代碼 :?https://gitee.com/lwx001/ShowFragment
RecyclerView 控件
導入recyclerView控件包
實戰演練
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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"tools:context=".MainActivity"><!--android.support.v7.widget.RecyclerView--><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/id_recyclerview"android:layout_width="match_parent"android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView> </RelativeLayout>ltem_home.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/iv_num"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/iv1" /><TextViewandroid:id="@+id/id_num"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="1" /> </LinearLayout>MainActivity.java
package cn.lwx.recyclerviewshow;import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;import android.os.Bundle;//import android.support.v7.app.AppCompatActivity; //import android.os.Bundle; //import android.support.v7.widget.LinearLayoutManager; //import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView;import java.util.ArrayList; import java.util.List;public class MainActivity extends AppCompatActivity {private RecyclerView mRecyclerView;private List<Integer> mDatas;private HomeAdapter mAdapter;private int[] img;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initData();mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));mRecyclerView.setAdapter(mAdapter = new HomeAdapter());}protected void initData() {mDatas = new ArrayList<Integer>();for (int i = 1; i < 11; i++) {mDatas.add(i);}img = new int[]{R.drawable.iv1, R.drawable.iv2,R.drawable.iv3, R.drawable.iv4, R.drawable.iv5,R.drawable.iv6, R.drawable.iv7, R.drawable.iv8,R.drawable.iv9, R.drawable.iv10};}class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {MyViewHolder holder = new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_home, parent,false));return holder;}@Overridepublic void onBindViewHolder(MyViewHolder holder, int position) {holder.tv.setText("這是第" + mDatas.get(position).toString() + "個精靈");holder.iv.setImageResource(img[position]);}@Overridepublic int getItemCount() {return mDatas.size();}class MyViewHolder extends RecyclerView.ViewHolder {TextView tv;ImageView iv;public MyViewHolder(View view) {super(view);tv = (TextView) view.findViewById(R.id.id_num);iv = (ImageView) view.findViewById(R.id.iv_num);}}} }?
下拉刷新
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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"tools:context=".MainActivity"><!--android.support.v4.widget.SwipeRefreshLayout--><androidx.swiperefreshlayout.widget.SwipeRefreshLayoutandroid:id="@+id/swipe_container"android:layout_width="match_parent"android:layout_height="match_parent"><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="70dp"android:gravity="center"android:text="下拉刷新"android:textSize="20sp" /></ScrollView></androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </RelativeLayout>MainActivity.java
package cn.lwx.swiperefresh;import androidx.appcompat.app.AppCompatActivity; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;import android.os.Bundle;import android.os.Handler; //import android.support.v4.widget.SwipeRefreshLayout; //import android.support.v7.app.AppCompatActivity; import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView tv;private SwipeRefreshLayout swipeRefreshLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv = (TextView) findViewById(R.id.textView1);swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light,android.R.color.holo_green_light,android.R.color.holo_blue_light);swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@Overridepublic void onRefresh() {tv.setText("正在刷新");new Handler().postDelayed(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtv.setText("刷新完成");swipeRefreshLayout.setRefreshing(false);}}, 3000);}});} }?
總結
以上是生活随笔為你收集整理的Android 高级编程【6个实战案例(附源码):刮刮卡、补间动画、逐帧动画、Fragment、RecyclerView、下拉刷新】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 假设用于通信的电文由字符集{a,b,c,
- 下一篇: 图论最短距离(Shortest Path