Android:方向更改会擦除对我的 ImageView 所做的修改
我有一个应用程序,其中有 ImageView。我打开新的活动,用手指画一些东西,这个位图返回到我的 ImageView。一切都很好,但是当我现在改变方向时,我的 ImageView 活动会重新绘制或重新启动,并且 imageview 为空。我尝试了一切,我尝试 SaveState 并恢复状态,尝试配置更改,等等,但没有任何效果......
package jilova.Android.TextFolder;
import java.io.ByteArrayOutputStream;
import jilova.Android.Enums;
import jilova.Android.R;
import jilova.Android.DatabaseFolder.LocalDB;
import jilova.Android.DatabaseFolder.RequestRow;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;
public class Text extends Activity{
private static EditText t1;
private static EditText t2;
private static EditText t3;
private static EditText t4;
private static ImageView iv1;
private static Context c;
private static Activity ac;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Object o = this.getLastNonConfigurationInstance();
if(o!=null)
{
Enums.sign=(Bitmap)o;
}
c=this.getApplicationContext();
ac=this;
ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
iv.setDrawingCacheEnabled(true);
t1 = (EditText)this.findViewById(R.id.TEXTNote);
t2 = (EditText)this.findViewById(R.id.TextET2);
t3 = (EditText)this.findViewById(R.id.TextET3);
t4 = (EditText)this.findViewById(R.id.TextET4);
//iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, iv.getWidth(), iv.getHeight(), false).copy(Config.ARGB_8888, true));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
}
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
}
}
public static RequestRow getData()
{
RequestRow row = new RequestRow();
row.SetREQUESTNOTE(t1.getText().toString());
row.SetREQUESTTYPEID(Integer.parseInt(t2.getTag().toString()));
row.SetTYPTECHUDRZBYID(Integer.parseInt(t3.getTag().toString()));
row.SetOBJECTID(Integer.parseInt(t4.getTag().toString()));
return row;
}
public static void setData(String REQUESTTYPEID ,String TYPTECHUDRZBYID,String OBJECTID,String REQUESTNOTE,String REQUESTID)
{
t1.setText(REQUESTNOTE);
t2.setTag(REQUESTTYPEID);
t2.setText(LocalDB.dbGetRequestTypeByID(c, Integer.parseInt(REQUESTTYPEID)));
t3.setTag(TYPTECHUDRZBYID);
t3.setText(LocalDB.dbGetTypTechUdrzbyByID(c, Integer.parseInt(TYPTECHUDRZBYID)));
t4.setTag(OBJECTID);
t4.setText(LocalDB.dbGetObjectByID(c, Integer.parseInt(OBJECTID)));
Bitmap b = LocalDB.dbGetDocumentByID(c, Integer.parseInt(REQUESTID));
if(b!=null)
{
iv1.setImageBitmap(b);
}
}
public static Bitmap getSign()
{
iv1.buildDrawingCache();
Bitmap ret = iv1.getDrawingCache();
if(Enums.EmptySignHash)
{
return null;
}
else
{
return ret;
}
}
public void Sign(View button)
{
Intent s = new Intent(Text.this,Sign.class);
startActivityForResult(s,Enums.SIGNREQUESTID);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode!=Activity.RESULT_OK)
{
return;
}
Enums.IDLocal=-1;
if(requestCode==Enums.GetData)
{
Bundle extras = data.getExtras();
if(extras !=null)
{
EditText et = (EditText)this.findViewById(extras.getInt("ViewID"));
et.setText(extras.getString("Value"));
et.setTag(extras.get("ID"));
}
}
else if(requestCode==Enums.SIGNREQUESTID)
{
Bundle extras = data.getExtras();
if(extras !=null)
{
ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
try
{
byte[] b = extras.getByteArray("Bitmap");
Enums.sign = Bitmap.createBitmap(BitmapFactory.decodeByteArray(b, 0, b.length), 0, 0, ac.getWindowManager().getDefaultDisplay().getWidth(), 200);
iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, iv.getWidth(), iv.getHeight(), false));
}
catch(Exception e)
{
int a=0;
}
}
Enums.EmptySignHash=false;
}
}
public static void DeleteAll()
{
EditText t = (EditText)ac.findViewById(R.id.TEXTNote);
t.setText("");
t.setTag(-1);
t = (EditText)ac.findViewById(R.id.TextET2);
t.setText("");
t.setTag(-1);
t = (EditText)ac.findViewById(R.id.TextET3);
t.setText("");
t.setTag(-1);
t = (EditText)ac.findViewById(R.id.TextET4);
t.setText("");
t.setTag(-1);
ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
//Bitmap b = Bitmap.createBitmap(Enums.createColors(), 0, Enums.STRIDE, Enums.WIDTH, Enums.HEIGHT, Bitmap.Config.ARGB_8888).copy(Bitmap.Config.ARGB_8888, true);
iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, ac.getWindowManager().getDefaultDisplay().getWidth(), iv.getLayoutParams().height, false));
Enums.EmptySignHash=true;
}
public void Delete(View button)
{
EditText t = (EditText)this.findViewById(R.id.TEXTNote);
t.setText("");
t.setTag(-1);
t = (EditText)this.findViewById(R.id.TextET2);
t.setText("");
t.setTag(-1);
t = (EditText)this.findViewById(R.id.TextET3);
t.setText("");
t.setTag(-1);
t = (EditText)this.findViewById(R.id.TextET4);
t.setText("");
t.setTag(-1);
ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
//Bitmap b = Bitmap.createBitmap(Enums.createColors(), 0, Enums.STRIDE, Enums.WIDTH, Enums.HEIGHT, Bitmap.Config.ARGB_8888).copy(Bitmap.Config.ARGB_8888, true);
iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, ac.getWindowManager().getDefaultDisplay().getWidth(), iv.getLayoutParams().height, false));
Enums.EmptySignHash=true;
}
public void GetData(View button)
{
if(button.getId()==R.id.TextIB3)
{
Intent ChooseAction = new Intent(Text.this,ChooseData.class);
Enums.Data = LocalDB.dbLocalSelect(this.getApplicationContext(),"Select typtechudrzbyid,typtechudrzbydesc from typtechudrzby");
if(Enums.Data==null)
{
Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
ChooseAction.putExtra("ViewID", R.id.TextET3);
startActivityForResult(ChooseAction,Enums.GetData);
}
else if(button.getId()==R.id.TextIB2)
{
Intent ChooseAction = new Intent(Text.this,ChooseData.class);
Enums.Data = LocalDB.dbLocalSelect(this.getApplicationContext(),"Select Requesttypeid,requestname from requesttype");
if(Enums.Data==null)
{
Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
ChooseAction.putExtra("ViewID", R.id.TextET2);
startActivityForResult(ChooseAction,Enums.GetData);
}
else if(button.getId()==R.id.TextIB4)
{
Intent ChooseAction = new Intent(Text.this,TreeData.class);
Enums.TreeData = LocalDB.dbLocalSelectTree(this.getApplicationContext(),"Select objectid,objectname,objectref from Objects");
if(Enums.TreeData==null)
{
Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
ChooseAction.putExtra("ViewID", R.id.TextET4);
startActivityForResult(ChooseAction,Enums.GetData);
}
else
{
//throw new Exception();
}
}
}
I have app in which I have ImageView. I open new activity, where I paint something by finger and this bitmap return to my ImageView. everything is ok but when I change orientation now, my activity with ImageView is repaint or restart and imageview is empty. I try everything, I try SaveState and restore state, try configurationChange, and other, but nothing is working...
package jilova.Android.TextFolder;
import java.io.ByteArrayOutputStream;
import jilova.Android.Enums;
import jilova.Android.R;
import jilova.Android.DatabaseFolder.LocalDB;
import jilova.Android.DatabaseFolder.RequestRow;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;
public class Text extends Activity{
private static EditText t1;
private static EditText t2;
private static EditText t3;
private static EditText t4;
private static ImageView iv1;
private static Context c;
private static Activity ac;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Object o = this.getLastNonConfigurationInstance();
if(o!=null)
{
Enums.sign=(Bitmap)o;
}
c=this.getApplicationContext();
ac=this;
ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
iv.setDrawingCacheEnabled(true);
t1 = (EditText)this.findViewById(R.id.TEXTNote);
t2 = (EditText)this.findViewById(R.id.TextET2);
t3 = (EditText)this.findViewById(R.id.TextET3);
t4 = (EditText)this.findViewById(R.id.TextET4);
//iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, iv.getWidth(), iv.getHeight(), false).copy(Config.ARGB_8888, true));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
}
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
}
}
public static RequestRow getData()
{
RequestRow row = new RequestRow();
row.SetREQUESTNOTE(t1.getText().toString());
row.SetREQUESTTYPEID(Integer.parseInt(t2.getTag().toString()));
row.SetTYPTECHUDRZBYID(Integer.parseInt(t3.getTag().toString()));
row.SetOBJECTID(Integer.parseInt(t4.getTag().toString()));
return row;
}
public static void setData(String REQUESTTYPEID ,String TYPTECHUDRZBYID,String OBJECTID,String REQUESTNOTE,String REQUESTID)
{
t1.setText(REQUESTNOTE);
t2.setTag(REQUESTTYPEID);
t2.setText(LocalDB.dbGetRequestTypeByID(c, Integer.parseInt(REQUESTTYPEID)));
t3.setTag(TYPTECHUDRZBYID);
t3.setText(LocalDB.dbGetTypTechUdrzbyByID(c, Integer.parseInt(TYPTECHUDRZBYID)));
t4.setTag(OBJECTID);
t4.setText(LocalDB.dbGetObjectByID(c, Integer.parseInt(OBJECTID)));
Bitmap b = LocalDB.dbGetDocumentByID(c, Integer.parseInt(REQUESTID));
if(b!=null)
{
iv1.setImageBitmap(b);
}
}
public static Bitmap getSign()
{
iv1.buildDrawingCache();
Bitmap ret = iv1.getDrawingCache();
if(Enums.EmptySignHash)
{
return null;
}
else
{
return ret;
}
}
public void Sign(View button)
{
Intent s = new Intent(Text.this,Sign.class);
startActivityForResult(s,Enums.SIGNREQUESTID);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode!=Activity.RESULT_OK)
{
return;
}
Enums.IDLocal=-1;
if(requestCode==Enums.GetData)
{
Bundle extras = data.getExtras();
if(extras !=null)
{
EditText et = (EditText)this.findViewById(extras.getInt("ViewID"));
et.setText(extras.getString("Value"));
et.setTag(extras.get("ID"));
}
}
else if(requestCode==Enums.SIGNREQUESTID)
{
Bundle extras = data.getExtras();
if(extras !=null)
{
ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
try
{
byte[] b = extras.getByteArray("Bitmap");
Enums.sign = Bitmap.createBitmap(BitmapFactory.decodeByteArray(b, 0, b.length), 0, 0, ac.getWindowManager().getDefaultDisplay().getWidth(), 200);
iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, iv.getWidth(), iv.getHeight(), false));
}
catch(Exception e)
{
int a=0;
}
}
Enums.EmptySignHash=false;
}
}
public static void DeleteAll()
{
EditText t = (EditText)ac.findViewById(R.id.TEXTNote);
t.setText("");
t.setTag(-1);
t = (EditText)ac.findViewById(R.id.TextET2);
t.setText("");
t.setTag(-1);
t = (EditText)ac.findViewById(R.id.TextET3);
t.setText("");
t.setTag(-1);
t = (EditText)ac.findViewById(R.id.TextET4);
t.setText("");
t.setTag(-1);
ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
//Bitmap b = Bitmap.createBitmap(Enums.createColors(), 0, Enums.STRIDE, Enums.WIDTH, Enums.HEIGHT, Bitmap.Config.ARGB_8888).copy(Bitmap.Config.ARGB_8888, true);
iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, ac.getWindowManager().getDefaultDisplay().getWidth(), iv.getLayoutParams().height, false));
Enums.EmptySignHash=true;
}
public void Delete(View button)
{
EditText t = (EditText)this.findViewById(R.id.TEXTNote);
t.setText("");
t.setTag(-1);
t = (EditText)this.findViewById(R.id.TextET2);
t.setText("");
t.setTag(-1);
t = (EditText)this.findViewById(R.id.TextET3);
t.setText("");
t.setTag(-1);
t = (EditText)this.findViewById(R.id.TextET4);
t.setText("");
t.setTag(-1);
ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
//Bitmap b = Bitmap.createBitmap(Enums.createColors(), 0, Enums.STRIDE, Enums.WIDTH, Enums.HEIGHT, Bitmap.Config.ARGB_8888).copy(Bitmap.Config.ARGB_8888, true);
iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, ac.getWindowManager().getDefaultDisplay().getWidth(), iv.getLayoutParams().height, false));
Enums.EmptySignHash=true;
}
public void GetData(View button)
{
if(button.getId()==R.id.TextIB3)
{
Intent ChooseAction = new Intent(Text.this,ChooseData.class);
Enums.Data = LocalDB.dbLocalSelect(this.getApplicationContext(),"Select typtechudrzbyid,typtechudrzbydesc from typtechudrzby");
if(Enums.Data==null)
{
Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
ChooseAction.putExtra("ViewID", R.id.TextET3);
startActivityForResult(ChooseAction,Enums.GetData);
}
else if(button.getId()==R.id.TextIB2)
{
Intent ChooseAction = new Intent(Text.this,ChooseData.class);
Enums.Data = LocalDB.dbLocalSelect(this.getApplicationContext(),"Select Requesttypeid,requestname from requesttype");
if(Enums.Data==null)
{
Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
ChooseAction.putExtra("ViewID", R.id.TextET2);
startActivityForResult(ChooseAction,Enums.GetData);
}
else if(button.getId()==R.id.TextIB4)
{
Intent ChooseAction = new Intent(Text.this,TreeData.class);
Enums.TreeData = LocalDB.dbLocalSelectTree(this.getApplicationContext(),"Select objectid,objectname,objectref from Objects");
if(Enums.TreeData==null)
{
Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
ChooseAction.putExtra("ViewID", R.id.TextET4);
startActivityForResult(ChooseAction,Enums.GetData);
}
else
{
//throw new Exception();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当默认情况下发生屏幕旋转等配置更改时,您的 Activity 将被销毁,然后重新创建(调用当前 Activity 的 onDestroy,然后调用新版本 Activity 的 onCreate)。
您可以:
android:configChanges="keyboardHidden|orientation"
添加到清单中的活动标记中。不建议这样做,因为如果您想要不同的布局等不同的配置,您将必须自己处理更改布局。onRetainNonConfigurationInstance
并从中返回您的位图。在 onCreate 中检查最后一个非配置实例是否不为 null,在这种情况下将其转换为位图,然后设置图像。对于后一种情况,请使用如下所示的内容:
When a configuration change such as a screen rotation occurs by default your Activity is destroyed and then recreated (onDestroy of the current activity is called, and then the onCreate of a new version of your activity is called).
You can either:
android:configChanges="keyboardHidden|orientation"
to the activity tag in your manifest. This is not recommend since if you want a different layout etc for different configurations you will have to handle changing the layout yourself.onRetainNonConfigurationInstance
and return your bitmap from that. InonCreate
check if the last non-configuration instance is not null, in which case cast it to a bitmap and then set the image.For the latter case, use something like the following:
我认为您忘记覆盖 onRetainNonConfigurationInstance() 返回位图的方法,以便将其传递给新活动。在新活动中,您可以像以前一样通过调用 getLastNonConfigurationInstance()。
I think you forgot to override the onRetainNonConfigurationInstance() method where you return your bitmap so it will be passed to the new activity. In the new activity you can retrieve the bitmap as you already do by calling getLastNonConfigurationInstance().
当配置更改时,将重新创建整个视图。所以,我们需要保留imageView资源。执行此操作的最佳方法是通过创建保留片段来处理方向更改。
您可以在 Android 开发者页面上找到完美的文档。
PS:强烈不建议将
android:configChanges="keyboardHidden|orientation"
添加到清单中。When the configuration is changed, the whole view is re-created. So, we need to retain the imageView resource. The best way to do this is to handle the orientation change by creating a Retained fragment.
You can find the perfect documentation on Android developer's page.
P.S.: Adding
android:configChanges="keyboardHidden|orientation"
to the manifest is highly not recommended.