加速球(內(nèi)存的變化)

clean.png

主界面

public class MainActivity extends AppCompatActivity implements  View.OnClickListener{
private ImageView imageView;
private SpeedView speedView;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.img_main);
    speedView = (SpeedView) findViewById(R.id.myview_main_speed);
    textView = (TextView) findViewById(R.id.tv_main);
    long available= Memory.availableMemory();//調(diào)取Memory的方法
    long total=Memory.totalMemory();
    double rote=(total-available)*0.8/total;
    int round= (int) (rote*360);
    int text= (int) (rote*100);
    speedView.setSweepAngle(round);
    textView.setText(text+" ");
    imageView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v.getId()==R.id.img_main||v.getId()==R.id.tv_main){
       //僅僅是為了能模擬,這里應(yīng)該為實(shí)際的值
        long available= Memory.availableMemory();
        long total=Memory.totalMemory();
        double rote=(total-available)*0.8/total*0.7;
        int round= (int) (rote*360);
        int text= (int) (rote*100);
        speedView.setSweepAngle(round);
        textView.setText(text+"");
    }
}
}

自己編譯的布局View,即變化的扇形

public class SpeedView extends View {
private RectF rectF;
private Paint paint;
private int sweepAngle=90;
private int a,data=0;
private boolean isrun;
private Timer timer;
private int[] dotiem={5,8,9,12,8,5};


public SpeedView(Context context) {
    //調(diào)用下面的
    this(context,null);
}

public SpeedView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs,0);
}

public SpeedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //畫(huà)筆
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.parseColor("#ff9736"));

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    //rect矩形
    rectF = new RectF(0,0,width, height);
    //設(shè)置
    setMeasuredDimension(width,height);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawArc(rectF,-90,sweepAngle,true,paint);
}

public void setSweepAngle(final int toAngle){

    //防止有人一直點(diǎn)
    if (isrun)return;
    //計(jì)時(shí)器
    timer = new Timer(false);
    TimerTask  timerTask=new TimerTask() {
        int goStar=0;
        @Override
        public void run() {
            switch (goStar){
                //退回去
                case 0:
                    isrun=true;
                    sweepAngle-=dotiem[data++];
                    if (data>=dotiem.length){
                        data=0;
                    }
                    if (sweepAngle<=0){
                        sweepAngle=0;
                        goStar=1;
                    }
                    break;
                //前進(jìn)
                case 1:
                    sweepAngle+=dotiem[data++];
                    if (data>=dotiem.length){
                        data=0;
                    }
                    if (sweepAngle>= toAngle) {
                        sweepAngle =  toAngle;
                        timer.cancel();
                        isrun=false;
                        data=0;
                        goStar = 0;

                    }
                    //刷新界面
                    postInvalidate();
                    break;

            }


        }
    };
    //代表每隔40毫秒停留50毫秒
    timer.schedule(timerTask,40,50);
}
}

加速球所在的布局。xml

 <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.bf.com.myapplication.MainActivity">
//自己編輯的扇形
<com.bf.com.myapplication.View.SpeedView
    android:id="@+id/myview_main_speed"
    android:layout_width="230dp"
    android:layout_height="230dp"
    android:layout_centerInParent="true"
    />

//自己定義的選擇器
<ImageView
   android:id="@+id/img_main"
   android:layout_width="220dp"
   android:layout_height="220dp"
   android:background="@drawable/round"
   android:layout_centerInParent="true"
   android:clickable="true"/>
<TextView
    android:id="@+id/tv_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="60dp"
    android:textStyle="bold"
    android:layout_centerInParent="true"/>
</RelativeLayout>

選擇器(寫(xiě)在res的drawable)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true"      android:drawable="@mipmap/home_score_normal_bg"/>
  <item android:state_pressed="false" android:drawable="@mipmap/home_score_pressed_bg"/>
</selector>

讀取內(nèi)存的代碼

public class Memory {
public static long availableMemory(){
    //想管理者請(qǐng)求
    ActivityManager  manager= (ActivityManager) App.appcontext.getSystemService(Context.ACTIVITY_SERVICE);
    //請(qǐng)求內(nèi)存信息
    ActivityManager.MemoryInfo memoryInfo=new ActivityManager.MemoryInfo();
    manager.getMemoryInfo(memoryInfo);
    //返回一個(gè)可用的內(nèi)存
    return memoryInfo.availMem;
}
public static long totalMemory(){
    //想管理者請(qǐng)求
    ActivityManager manager = (ActivityManager) App.appcontext.getSystemService(Context.ACTIVITY_SERVICE);
    //請(qǐng)求內(nèi)存信息
    ActivityManager.MemoryInfo memoryInfo=new ActivityManager.MemoryInfo();
    manager.getMemoryInfo(memoryInfo);
    //返回一個(gè)總的內(nèi)存
    return memoryInfo.totalMem;
}
}

APP承接上下文的,新建java

public class App extends Application{
public static App appcontext;
@Override
public void onCreate() {
    super.onCreate();
    appcontext=this;
}
 }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,377評(píng)論 25 708
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線(xiàn)程,因...
    小菜c閱讀 6,535評(píng)論 0 17
  • Day1: 在代碼中通過(guò)R.string.hello_world可以獲得該字符串的引用; 在XML中通過(guò)@stri...
    冰凝雪國(guó)閱讀 1,451評(píng)論 0 5
  • 我愛(ài)你 想對(duì)你說(shuō)一句 愛(ài)你寬闊的肩膀 愛(ài)你微笑的臉龐 愛(ài)你善意的說(shuō)謊 可這些都是我的心事 無(wú)法向任何人表明 時(shí)間越...
    安靜的復(fù)蘇918閱讀 226評(píng)論 4 5
  • 來(lái)簡(jiǎn)書(shū)一個(gè)多月了,簡(jiǎn)書(shū)到底帶給了我一些什么? 學(xué)會(huì)了堅(jiān)持。雖然做不到每天更新一篇文章,但總能斷斷續(xù)續(xù)地出作品,一個(gè)...
    陶語(yǔ)閱讀 717評(píng)論 24 11