从数据存储到属性动画

数据存储的几种方式:

布局:




  

 

因为sdk版本在23以后需要动态申请系统读写权限,所以首先在Mainfest配置里加上:



然后在APP启动时加上权限

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);

1.SharedPreferences:

public class SharedPreferencesActivity extends AppCompatActivity {

    private EditText ev_name;
    private Button save,show;
    private TextView content;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
        ev_name = findViewById(R.id.et_name);
        save = findViewById(R.id.btn_save);
        show = findViewById(R.id.btn_show);
        content = findViewById(R.id.tv_show);

        sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        editor = sharedPreferences.edit();

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor.putString("name",ev_name.getText().toString());
                editor.apply();
            }
        });

        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content.setText(sharedPreferences.getString("name",""));
            }
        });
    }
}

2.File内部存储

public class FileActivity extends AppCompatActivity {

    private EditText ev_name;
    private Button save,show;
    private TextView content;
    private final String filename = "test.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);
        ev_name = findViewById(R.id.et_name);
        save = findViewById(R.id.btn_save);
        show = findViewById(R.id.btn_show);
        content = findViewById(R.id.tv_show);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save(ev_name.getText().toString().trim());
            }
       });

        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content.setText(read());
            }
        });
    }
    //存储数据
    private void save(String content){
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = openFileOutput(filename,MODE_PRIVATE);
            fileOutputStream.write(content.getBytes());
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
       }
   }
    //读取数据
    private String read(){
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = openFileInput(filename);
            byte[] buff = new byte[1024];
            StringBuffer sb = new StringBuffer();   //可实现字符串拼接
            Log.d("path",Environment.getDataDirectory()+"");
            int len = 0;
            while((len = fileInputStream.read(buff))>0){
                sb.append(new String(buff,0,len));
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            {
                if(fileInputStream != null){
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }
}

3.file外部存储:

public class FileOutActivity extends AppCompatActivity {

    private EditText ev_name;
    private Button save,show;
    private TextView content;
    private final String filename = "test.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_out);
        ev_name = findViewById(R.id.et_name);
        save = findViewById(R.id.btn_save);
        show = findViewById(R.id.btn_show);
        content = findViewById(R.id.tv_show);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save(ev_name.getText().toString().trim());
            }
        });

        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content.setText(read());
            }
        });
    }
    //存储数据
    private void save(String content){
        FileOutputStream fileOutputStream = null;
        try {
//          fileOutputStream = openFileOutput(filename,MODE_PRIVATE);
            File dir = new File(Environment.getExternalStorageDirectory(),"peng");
            if(!dir.exists()){
                dir.mkdirs();       //mkdir,只新建一个文件夹,mkdirs,会将/后面的都新建
            }
            File file = new File(dir,filename);
            if(!file.exists()){
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(content.getBytes());
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //读取数据
    private String read(){
        FileInputStream fileInputStream = null;

        try {
//          fileInputStream = openFileInput(filename);
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"peng",filename);
            Log.d("path",Environment.getExternalStorageDirectory().getAbsolutePath());
            fileInputStream = new FileInputStream(file);
            byte[] buff = new byte[1024];
            StringBuffer sb = new StringBuffer();   //可实现字符串拼接
            int len = 0;
            while((len = fileInputStream.read(buff))>0){
                sb.append(new String(buff,0,len));
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            {
               if(fileInputStream != null){
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }
}

广播

activity_broad.xml:




  

activity_broad2.xml:




    

代码:

public class BroadActivity extends AppCompatActivity {

    private Button btn_intent;
    private TextView tv_test;
    private MyBroadcast myBroadcast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad);
        btn_intent = findViewById(R.id.btn_intent);
        tv_test = findViewById(R.id.tv_test);

        btn_intent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BroadActivity.this,BroadActivity2.class);
                startActivity(intent);
            }
        });

        myBroadcast = new MyBroadcast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.examle.learn.action");
        LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcast,intentFilter);
    }

    private class MyBroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
           if(intent.getAction() != null){
               switch (intent.getAction()){
                   case "com.examle.learn.action":
                       tv_test.setText("123");
                       break;
               }
           }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcast);
    }
}

public class BroadActivity2 extends AppCompatActivity {

    private Button btn_broad1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad2);
        btn_broad1 = findViewById(R.id.btn_broad1);

        btn_broad1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.examle.learn.action");
                LocalBroadcastManager.getInstance(BroadActivity2.this).sendBroadcast(intent);
            }
        });
    }
}

属性动画

public class ObjectAnimateActivity extends AppCompatActivity {

private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_object_animate);
    tv = findViewById(R.id.tv);
//1.
//        tv.animate().translationY(500).setDuration(2000).start();


//2.
    ValueAnimator valueAnimator = ValueAnimator.ofInt(100,500);
    valueAnimator.setDuration(1000);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
//                valueAnimator实际的值
            Log.d("animate",animation.getAnimatedValue()+"");
//                动画的进度0-1
            Log.d("animate",animation.getAnimatedFraction()+"");
            ViewGroup.LayoutParams params = tv.getLayoutParams();
            params.height = (int) animation.getAnimatedValue();
            tv.setLayoutParams(params);
            tv.setText(animation.getAnimatedValue()+"");

        }
    });
    valueAnimator.start();

//3.
    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv,"translationY",0,500,200,800);
    objectAnimator.setDuration(2000);
    objectAnimator.start();
}
}

你可能感兴趣的:(从数据存储到属性动画)