我有app我在其中有ImageView。我打开新的活动,用手指画一些东西,这个位图返回到我的ImageView。一切正常但是当我现在改变方向时,我对ImageView的活动是重绘或重新启动,而imageview是空的。我尝试了一切,我尝试SaveState并恢复状态,尝试configureChange等,但没有任何工作......
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();
}
}
}
答案 0 :(得分:1)
我认为你有link的解决方案。
https://stackoverflow.com/questions/456211/activity-restart-on-rotation-android
答案 1 :(得分:1)
当默认情况下发生屏幕旋转等配置更改时,您的Activity将被销毁,然后重新创建(调用当前活动的onDestroy,然后调用活动新版本的onCreate)。
你可以:
android:configChanges="keyboardHidden|orientation"
添加到清单中的活动代码。建议不要这样做,因为如果您想为不同的配置使用不同的布局等,您必须自己处理更改布局。onRetainNonConfigurationInstance
并从中返回您的位图。在onCreate
中检查最后一个非配置实例是否为空,在这种情况下将其转换为位图,然后设置图像。对于后一种情况,请使用以下内容:
@Override
public void onCreate(Bundle savedInstanceState) {
...
// Check if our activity was just destroyed and re-created
final Object retainedFromConfigChange = getLastNonConfigurationInstance();
if (retainedFromConfigChange != null) {
// Activity has just been recreated, get the image we were working on
// before the configuration change
ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
iv.setImageBitmap((Bitmap) retainedFromConfigChange);
}
...
}
@Override
public Object getLastNonConfigurationInstance() {
ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
// We have to return a plain old Bitmap and not a drawable of any sorts
// or we will get memory leaks so we need to extract the bitmap from the drawable
return ((BitmapDrawable) iv.getDrawable()).getBitmap();
}
答案 2 :(得分:0)
我认为您忘记覆盖返回位图的onRetainNonConfigurationInstance()方法,以便将其传递给新活动。在新活动中,您可以通过调用getLastNonConfigurationInstance()来检索位图。
答案 3 :(得分:0)
更改配置后,将重新创建整个视图。因此,我们需要保留imageView资源。执行此操作的最佳方法是通过创建保留片段来处理方向更改。
您可以在Android developer's page找到完美的文档。
P.S。:强烈建议不要在清单中添加android:configChanges="keyboardHidden|orientation"
。