Android 如何通过考虑视图类将位图分配给布局中作为图像视图的子级?

Android 如何通过考虑视图类将位图分配给布局中作为图像视图的子级?,android,view,bitmap,children,Android,View,Bitmap,Children,我有一个应用程序,我想通过获取图像视图在布局中的位置(这里是位置0处的子对象),将名为original的位图分配给布局中id为img1的图像视图。当我单击“保存”按钮时,我想将名为“原始”的位图分配给布局中id为img1的图像视图。我知道我们可以使用ourImageView.setImageBitmap(bitmaptobeassigned)进行分配。但在我的应用程序中,我的情况是仅通过查看对象子对象来分配位图原件。请帮助我。我的主课是 import android.app.Activity

我有一个应用程序,我想通过获取图像视图在布局中的位置(这里是位置0处的子对象),将名为original的位图分配给布局中id为img1的图像视图。当我单击“保存”按钮时,我想将名为“原始”的位图分配给布局中id为img1的图像视图。我知道我们可以使用ourImageView.setImageBitmap(bitmaptobeassigned)进行分配。但在我的应用程序中,我的情况是仅通过查看对象子对象来分配位图原件。请帮助我。我的主课是

  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); 
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);
//background.setImageBitmap(original);
save.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

            ImageView child=(ImageView) frame.getChildAt(0); 
            child.setImageBitmap(original);



    }
});

 }
  }
//这里是我小时候获得的图像视图。但我不知道如何为其分配位图。
//请帮我在这里写代码

      }
  });

  }
   }  
我的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>

我找到了答案。我的主要课程是

  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); 
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);
//background.setImageBitmap(original);
save.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

            ImageView child=(ImageView) frame.getChildAt(0); 
            child.setImageBitmap(original);



    }
});

 }
  }
我的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>