-1
我有一個應用程序,我想通過在佈局中的圖像視圖的位置(這裏它是位於0的子節點),在佈局中將一個名爲original的位圖分配給其id爲img1的圖像視圖。當我點擊保存按鈕時,我想將位圖命名爲原始的圖像視圖的id爲img1在layout.I知道我們可以通過使用ourImageView.setImageBitmap(bitmaptobeassigned)分配。但在我的應用程序中,我的情況是僅通過查看對象子對象來分配原始位圖。請幫助我。我的主要類是如何通過考慮View類將位圖分配給作爲佈局中圖像視圖的子級?
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Test extends Activity
{
Button save;
Bitmap original;
FrameLayout frame;
ImageView background;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
save=(Button)findViewById(R.id.button3);
frame=(FrameLayout)findViewById(R.id.frame);
background=(ImageView)findViewById(R.id.img1);
background.setOnTouchListener(new myTouchListener());
original=BitmapFactory.decodeResource(getResources(), R.drawable.parrot);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View child=frame.getChildAt(0);
//這裏我作爲一個孩子獲得圖像視圖。但我不知道如何分配位圖。
//請幫助我如何在這裏編寫代碼。
}
});
}
}
和我的XML文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/frame"
>
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>