Java 如何在android中共享图像和文本视图

Java 如何在android中共享图像和文本视图,java,android,share,Java,Android,Share,我的代码只共享图像,而不共享用户将图像放在图像上的文本视图。 我想通过任何应用程序图像和文本一起共享邀请卡。不仅仅是图像 所以我的问题是如何共享这个名为g7的图像和文本视图,它们的id是tvView 注意,用户可以通过setOnTouchListener方法更改textview的位置 这是我的g7.java代码 public class g7 extends ActionBarActivity { Button button; TextView tvView ; TextView red ;

我的代码只共享图像,而不共享用户将图像放在图像上的文本视图。 我想通过任何应用程序图像和文本一起共享邀请卡。不仅仅是图像

所以我的问题是如何共享这个名为g7的图像和文本视图,它们的id是tvView

注意,用户可以通过setOnTouchListener方法更改textview的位置

这是我的g7.java代码

public class g7 extends ActionBarActivity {
Button button;
TextView tvView ; 
TextView red ;
TextView green ;
TextView blue ;
TextView yellow ;
TextView purple ;
TextView pink ;
TextView tv ; 
  float x,y = 0.0f ; 
    boolean moving = false ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.g7);
    tvView = (TextView) findViewById(R.id.tvView) ;

    red = (TextView) findViewById(R.id.red) ;
    green = (TextView) findViewById(R.id.green) ;
    blue = (TextView) findViewById(R.id.blue) ;
    yellow = (TextView) findViewById(R.id.yellow) ;
    purple = (TextView) findViewById(R.id.purple) ;
    pink = (TextView) findViewById(R.id.pink) ;
    Intent  intent = getIntent(); 
    String texx = intent.getStringExtra("fname") ; 
    tvView.setText(texx);
    tvView.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View arg0 , MotionEvent arg1) {
            switch (arg1.getAction())
            {
            case MotionEvent.ACTION_DOWN : 
                moving = true ; 
                break ; 
            case MotionEvent.ACTION_MOVE : 
                if (moving) 
                {
                    x = arg1.getRawX()- tvView.getWidth()/2  ; 
                    y = arg1.getRawY() - tvView.getHeight() * 3/2 ; 
                    tvView.setX(x) ; 
                    tvView.setY(y) ; 

                }
                break ; 

            case MotionEvent.ACTION_UP : 
            moving = false ; 
                break ; 
            }
    return true;
    }
 });
    red.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View arg2 , MotionEvent arg3) {
            if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
                tvView.setTextColor(getResources().getColor(R.color.Red)) ;
        }


    return true;
    }
 });
    green.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View arg2 , MotionEvent arg3) {
            if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
                tvView.setTextColor(getResources().getColor(R.color.Green)) ;
        }


    return true;
    }
 });
    blue.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View arg2 , MotionEvent arg3) {
            if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
                tvView.setTextColor(getResources().getColor(R.color.Blue)) ;
        }


    return true;
    }
 });
    yellow.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View arg2 , MotionEvent arg3) {
            if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
                tvView.setTextColor(getResources().getColor(R.color.Yellow)) ;
        }


    return true;
    }
 });
    purple.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View arg2 , MotionEvent arg3) {
            if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
                tvView.setTextColor(getResources().getColor(R.color.Purple)) ;
        }


    return true;
    }
 });
    pink.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View arg2 , MotionEvent arg3) {
            if (arg3.getAction() == MotionEvent.ACTION_DOWN) {
                tvView.setTextColor(getResources().getColor(R.color.Pink)) ;
        }


    return true;
    }
 });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_settings) {

        return true;
    }

        if (id == R.id.share)
        { 

            PackageManager pm=getPackageManager();

            Intent waIntent = new Intent(Intent.ACTION_SEND);
            waIntent.setType("text/plain");




                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("image/*");
                Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+"drawable/g7");
                intent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(intent, "Share via"));




            return true;}


    return super.onOptionsItemSelected(item);
}
}

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >

<LinearLayout
android:id = "@+id/bottomlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".g7" >



<ImageView
    android:id="@+id/g7"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/g7" />

</LinearLayout >
<LinearLayout
 android:id = "@+id/toplinear"
  android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".g7">
<TextView
    android:id="@+id/tvView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />
 <TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
 <TextView
        android:id="@+id/tvv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text color :"
        />
    <TextView
        android:id="@+id/red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="red    "
        android:textColor="@color/Red" />

    <TextView
        android:id="@+id/green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="green    "
        android:textColor="@color/Green" />

    <TextView
        android:id="@+id/blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="blue    " 
        android:textColor="@color/Blue"/>

    <TextView
        android:id="@+id/yellow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="yellow    "
        android:textColor="@color/Yellow" />

    <TextView
        android:id="@+id/purple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="purple    " 
        android:textColor="@color/Purple"/>
    <TextView
        android:id="@+id/pink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="pink    "
        android:textColor="@color/Pink" />

 </TableRow>


 </TableLayout>
 </LinearLayout>
 </RelativeLayout>

我假设xml文件中的RelativeLayout代表整个邀请卡,对吗?如果是:

基本战略:

1将邀请卡渲染为位图

2共享渲染的位图

对于渲染卡,可以使用类似以下的函数。它将视图渲染到位图上RelativeLayout类是View类的孙子:

public Bitmap renderViewAsBitmap( View view ) {
    view.setDrawingCacheEnabled( true );
    view.buildDrawingCache();
    return view.getDrawingCache();
}
为了保存位图,您可以使用类似这样的函数。它将PNG文件输出到指定路径:

public boolean saveBitmap( Bitmap bitmap, String filepath ) {
    try {
        FileOutputStream out = new FileOutputStream( filepath );
        bitmap.compress( Bitmap.CompressFormat.PNG, 100, out );
        out.flush();
        out.close();
    } catch( Exception e ) {
        Log.e( "g7", "Failed to save bitmap", e );
        return false;
    }
    return true;
}
要生成用于共享的URI,可以使用类似以下的函数

public void shareImageFile( File file ) {
    Uri uri = Uri.fromFile( file );
    Intent intent = new Intent();
    intent.setAction( Intent.ACTION_SEND );
    intent.setType( "image/*" );
    intent.putExtra( Intent.EXTRA_STREAM, uri );
    startActivity( Intent.createChooser( intent, "Share via" ) );
}
您可以将上述函数放入g7类中。然后,当您准备好共享卡时,使用它们生成位图,将其保存到文件,并启动共享意图:

    Bitmap cardBitmap = renderViewAsBitmap( findViewById( R.id.cardlayout ) );

    // Do your own file management.  Here I just delete and reuse same file.
    String cardFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/Android/data/" + getPackageName() + "/";
    File cardFolder = new File( cardFolderPath );
    cardFolder.mkdirs();

    String cardPath = cardFolderPath + "card.png";
    File cardFile = new File( cardPath );
    if( cardFile.exists() ) {
        cardFile.delete();
    }

    if( saveBitmap( cardBitmap, cardPath ) ) {
        shareImageFile( cardFile );
    } else {
        // TODO: Error handling
    }
几点注意:

以上要求您的应用程序具有写入外部存储的权限:如果您不希望,可以将其更改为使用内部存储。在AndroidManifest.xml中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

你还需要给你的亲戚一个id,我在上面的例子中使用了@+id/cardlayout

必须编写一段代码作为邀请卡一起共享图像和文本视图,我不知道怎么做!!谢谢thank,但请告诉我应该在哪里添加这段公共位图renderViewAsBitmap View{View.setDrawingCacheEnabled true;View.buildDrawingCache;return View.getDrawingCache;}以及如何创建局部变量->文件???@Dua'a Shloul,当然今晚晚些时候我会充实我的答案。