想把拉取user/patient张图片的冗余方法合并成一个方法
Want to combine redundant Methods of pulling user/patient images into one method
我在 xml 布局中设置了九个 ImageView。我的应用程序直接从设备上的文件夹中提取图像并将图像加载到那些 ImageView 上。由于冗余,我的应用程序正在减慢我添加的 patients/users 的速度。我确定有办法将这些方法(做完全相同的事情)组合成一个方法,我只是不确定如何,我正在考虑创建一个文件数组。有任何想法吗?
这是我关于声明和实例化九个 ImageView 的代码
ImageView patientOne;
ImageView patientTwo;
ImageView patientThree;
ImageView patientFour;
ImageView patientFive;
ImageView patientSix;
ImageView patientSeven;
ImageView patientEight;
ImageView patientNine;
public void initializeViews() {
patientOne= (ImageView)findViewById(R.id.patient_one);
patientTwo= (ImageView)findViewById(R.id.patient_two);
patientThree= (ImageView)findViewById(R.id.patient_three);
patientFour= (ImageView)findViewById(R.id.patient_four);
patientFive= (ImageView)findViewById(R.id.patient_five);
patientSix= (ImageView)findViewById(R.id.patient_six);
patientSeven= (ImageView)findViewById(R.id.patient_seven);
patientEight= (ImageView)findViewById(R.id.patient_eight);
patientNine= (ImageView)findViewById(R.id.patient_nine);
newPatient = (Button)findViewById(R.id.new_patient);
}
这些是我从特定对应目录中提取患者s/user图像的九个冗余命令
public void getPatientOne() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient1/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientOne.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientTwo() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient2/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientTwo.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientThree() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient3/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientThree.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientFour() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient4/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientFour.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientFive() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient5/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientFive.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientSix() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient6/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientSix.setImageURI(Uri.fromFile(imgFile));
}
}
//And so on and so forth until I have nine Patients
}//end of class
这些是在我的 onCreate 方法中调用的方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
initializeViews();
getPatientOne();
getPatientTwo();
getPatientThree();
getPatientFour();
getPatientFive();
getPatientSix();
}
如果你很好奇,这就是我创建患者文件夹的方式
// Name of Folder = Patient's Number + randomInt
public void createPatientFolder() {
count=count+1;
StringBuilder sb = new StringBuilder();
sb.append("/PerinatalMonitor/Patient");
sb.append(count);
testname= sb.toString();
SharedPreferences pinfo = getSharedPreferences("pf",Context.MODE_PRIVATE);
SharedPreferences.Editor editor= pinfo.edit();
editor.clear();
editor.putString("name", testname);
editor.putBoolean("safe",false);
editor.commit();
patientFolder = new File(Environment.getExternalStorageDirectory() + testname);
boolean success= true;
if(patientFolder.exists()){
Toast toast = Toast.makeText(this, "Already Exists", Toast.LENGTH_LONG);
toast.show();
createPatientFolder();
}
if(!patientFolder.exists()) {
success = patientFolder.mkdirs();
Toast toast = Toast.makeText(this, "pass", Toast.LENGTH_SHORT);
toast.show();
}
if (success) {
Toast toast = Toast.makeText(this, "pass", Toast.LENGTH_SHORT);
toast.show();
}
else {
Toast toast = Toast.makeText(this, "fail", Toast.LENGTH_SHORT);
toast.show();
}
}
我建议您使用 Imageview 数组。 getPatientXX() 方法的唯一区别是文件夹编号,即 Patient1、Patient2 等。
循环遍历图像视图并设置图像路径很容易。
public void getPatients() {
for(int i=0;i<patient.length;i++)
{
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient"+i+"/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patient[i].setImageURI(Uri.fromFile(imgFile));
}
}
}
其中,patient为imageview数组。此方法将遍历整个 imageview 数组,从文件夹中获取图像。
我在 xml 布局中设置了九个 ImageView。我的应用程序直接从设备上的文件夹中提取图像并将图像加载到那些 ImageView 上。由于冗余,我的应用程序正在减慢我添加的 patients/users 的速度。我确定有办法将这些方法(做完全相同的事情)组合成一个方法,我只是不确定如何,我正在考虑创建一个文件数组。有任何想法吗?
这是我关于声明和实例化九个 ImageView 的代码
ImageView patientOne;
ImageView patientTwo;
ImageView patientThree;
ImageView patientFour;
ImageView patientFive;
ImageView patientSix;
ImageView patientSeven;
ImageView patientEight;
ImageView patientNine;
public void initializeViews() {
patientOne= (ImageView)findViewById(R.id.patient_one);
patientTwo= (ImageView)findViewById(R.id.patient_two);
patientThree= (ImageView)findViewById(R.id.patient_three);
patientFour= (ImageView)findViewById(R.id.patient_four);
patientFive= (ImageView)findViewById(R.id.patient_five);
patientSix= (ImageView)findViewById(R.id.patient_six);
patientSeven= (ImageView)findViewById(R.id.patient_seven);
patientEight= (ImageView)findViewById(R.id.patient_eight);
patientNine= (ImageView)findViewById(R.id.patient_nine);
newPatient = (Button)findViewById(R.id.new_patient);
}
这些是我从特定对应目录中提取患者s/user图像的九个冗余命令
public void getPatientOne() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient1/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientOne.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientTwo() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient2/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientTwo.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientThree() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient3/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientThree.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientFour() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient4/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientFour.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientFive() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient5/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientFive.setImageURI(Uri.fromFile(imgFile));
}
}
public void getPatientSix() {
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient6/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patientSix.setImageURI(Uri.fromFile(imgFile));
}
}
//And so on and so forth until I have nine Patients
}//end of class
这些是在我的 onCreate 方法中调用的方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
initializeViews();
getPatientOne();
getPatientTwo();
getPatientThree();
getPatientFour();
getPatientFive();
getPatientSix();
}
如果你很好奇,这就是我创建患者文件夹的方式
// Name of Folder = Patient's Number + randomInt
public void createPatientFolder() {
count=count+1;
StringBuilder sb = new StringBuilder();
sb.append("/PerinatalMonitor/Patient");
sb.append(count);
testname= sb.toString();
SharedPreferences pinfo = getSharedPreferences("pf",Context.MODE_PRIVATE);
SharedPreferences.Editor editor= pinfo.edit();
editor.clear();
editor.putString("name", testname);
editor.putBoolean("safe",false);
editor.commit();
patientFolder = new File(Environment.getExternalStorageDirectory() + testname);
boolean success= true;
if(patientFolder.exists()){
Toast toast = Toast.makeText(this, "Already Exists", Toast.LENGTH_LONG);
toast.show();
createPatientFolder();
}
if(!patientFolder.exists()) {
success = patientFolder.mkdirs();
Toast toast = Toast.makeText(this, "pass", Toast.LENGTH_SHORT);
toast.show();
}
if (success) {
Toast toast = Toast.makeText(this, "pass", Toast.LENGTH_SHORT);
toast.show();
}
else {
Toast toast = Toast.makeText(this, "fail", Toast.LENGTH_SHORT);
toast.show();
}
}
我建议您使用 Imageview 数组。 getPatientXX() 方法的唯一区别是文件夹编号,即 Patient1、Patient2 等。
循环遍历图像视图并设置图像路径很容易。
public void getPatients() {
for(int i=0;i<patient.length;i++)
{
String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient"+i+"/patient.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
patient[i].setImageURI(Uri.fromFile(imgFile));
}
}
}
其中,patient为imageview数组。此方法将遍历整个 imageview 数组,从文件夹中获取图像。