加速球(内存的变化)
2017-04-21 本文已影响0人
谜之龙
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();//调取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){
//仅仅是为了能模拟,这里应该为实际的值
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) {
//调用下面的
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);
//画笔
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);
//设置
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){
//防止有人一直点
if (isrun)return;
//计时器
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;
//前进
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>
选择器(写在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>
读取内存的代码
public class Memory {
public static long availableMemory(){
//想管理者请求
ActivityManager manager= (ActivityManager) App.appcontext.getSystemService(Context.ACTIVITY_SERVICE);
//请求内存信息
ActivityManager.MemoryInfo memoryInfo=new ActivityManager.MemoryInfo();
manager.getMemoryInfo(memoryInfo);
//返回一个可用的内存
return memoryInfo.availMem;
}
public static long totalMemory(){
//想管理者请求
ActivityManager manager = (ActivityManager) App.appcontext.getSystemService(Context.ACTIVITY_SERVICE);
//请求内存信息
ActivityManager.MemoryInfo memoryInfo=new ActivityManager.MemoryInfo();
manager.getMemoryInfo(memoryInfo);
//返回一个总的内存
return memoryInfo.totalMem;
}
}
APP承接上下文的,新建java
public class App extends Application{
public static App appcontext;
@Override
public void onCreate() {
super.onCreate();
appcontext=this;
}
}