您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在Android中通過自定義控件實現水波紋效果,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
MainActivity.java
package com.example.mhy.shuibowen; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
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="com.example.mhy.shuibowen.MainActivity"> <com.example.mhy.shuibowen.MyRingWave android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
MyRingWave.java
package com.example.mhy.shuibowen; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Handler; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import java.util.ArrayList; /** * 水波紋效果 * Created by mhy on 2016/6/16. */ public class MyRingWave extends View { /** * 二個相臨波浪中心點的最小距離 */ private static final int DIS_SOLP = 13; protected boolean isRunning = false; private ArrayList<Wave> wList; public MyRingWave(Context context,AttributeSet attrs) { super(context, attrs); wList = new ArrayList<MyRingWave.Wave>(); } @Override protected void onDraw(Canvas canvas) { for(int i=0; i<wList.size(); i++) { Wave wave = wList.get(i); canvas.drawCircle(wave.cx, wave.cy, wave.r, wave.p); } } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: int x = (int) event.getX(); int y = (int) event.getY(); addPoint(x, y); break; default: break; } return true; } private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { //刷新數據 flushData(); //刷新頁面 invalidate(); //循環動畫 if (isRunning) { handler.sendEmptyMessageDelayed(0, 50); } } }; /** * 刷新數據 */ private void flushData() { for (int i = 0; i < wList.size(); i++) { Wave w = wList.get(i); //如果透明度為 0 從集合中刪除 int alpha = w.p.getAlpha(); if(alpha == 0){ wList.remove(i); //刪除i 以后,i的值應該再減1 否則會漏掉一個對象,不過,在此處影響不大,效果上看不出來。 continue; } alpha-=5; if(alpha<5){ alpha =0; } //降低透明度 w.p.setAlpha(alpha); //擴大半徑 w.r = w.r+3; //設置半徑厚度 w.p.setStrokeWidth(w.r/3); } /* * 如果集合被清空,就停止刷新動畫 */ if(wList.size() == 0){ isRunning = false; } } /** * 添加新的波浪中心點 * @param x * @param y */ private void addPoint(int x, int y) { if(wList.size() == 0) { addPoint2List(x, y); isRunning = true; handler.sendEmptyMessage(0); }else{ Wave w = wList.get(wList.size()-1); if(Math.abs(w.cx - x)>DIS_SOLP || Math.abs(w.cy-y)>DIS_SOLP){ addPoint2List(x,y); } }; } /** * 添加新的波浪 * @param x * @param y */ private void addPoint2List(int x, int y) { Wave w = new Wave(); w.cx = x; w.cy=y; Paint pa=new Paint(); pa.setColor(colors[(int)(Math.random()*4)]); pa.setAntiAlias(true); pa.setStyle(Paint.Style.STROKE); w.p = pa; wList.add(w); } private int [] colors = new int[]{Color.BLUE,Color.RED,Color.YELLOW,Color.GREEN}; private class Wave { //圓心 int cx; int cy; //畫筆 Paint p; //半徑 int r; } }
MyRing.java
package com.example.mhy.shuibowen; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by mhy on 2016/6/16. */ public class MyRing extends View { /** * 圓心的X坐標 */ private float cx; /** * 圓心的Y坐標 */ private float cy; /** * 圓環半徑 */ private float radius = 0; /** * 默認畫筆 */ private Paint paint; private boolean isRuning = false; public MyRing(Context context, AttributeSet attrs) { super(context, attrs); initView(); } private void initView() { radius = 0; paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); // 空心圓 paint.setStrokeWidth(radius / 4); // 畫筆寬度 半徑4分之一 paint.setColor(Color.GREEN); // 畫筆顏色 paint.setAlpha(255); //不透明 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if(changed) { cx = getWidth() / 2; cy = getHeight() / 2; } } @Override protected void onDraw(Canvas canvas) { canvas.drawCircle(cx, cy, radius, paint); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); isRuning = false; } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); if(event.getAction() == MotionEvent.ACTION_DOWN) { cx = event.getX(); cy = event.getY(); initView(); startAnim(); } return true; } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { // 設置透明度 int alpha = paint.getAlpha(); if(alpha == 0) { isRuning = false; } // 透明度 慢慢變透明 alpha = Math.max(0, alpha-10); paint.setAlpha(alpha); System.out.println(alpha); // 設置半徑 radius += 5; paint.setStrokeWidth(radius / 3); invalidate(); if(isRuning) { handler.sendEmptyMessageDelayed(0, 50); } } }; private void startAnim() { isRuning = true; handler.sendEmptyMessageDelayed(0, 50); } }
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
看完上述內容,你們對怎么在Android中通過自定義控件實現水波紋效果有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。