my code share the image only without textView which user put it over image . i want to share invitation card via any application (image and text together) . not image only
so my problem is how to share this image which name g7 and textview which have id (tvView ) over image
notice that the user can change the position of textview by setOnTouchListener() method
this is my g7.java code
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);
}
}
and this my xml file
<?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>
I assume the RelativeLayout in your xml file represents the whole invitation card, correct? If so:
Basic Strategy:
1) Render the invitation card to a bitmap
2) Share the rendered bitmap
For rendering the card , you can use a function something like this. It renders a View onto a Bitmap (the RelativeLayout class is a grandchild of the View class):
public Bitmap renderViewAsBitmap( View view ) {
view.setDrawingCacheEnabled( true );
view.buildDrawingCache();
return view.getDrawingCache();
}
For saving the Bitmap , you can use a function something like this. It outputs a PNG file to the specified path:
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;
}
To generate the URI for sharing , you can use a function something like this.
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" ) );
}
You could place the above functions into your g7 class. Then when you are ready to share the card, use them to generate a Bitmap, save it to file, and start the sharing intent:
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
}
A couple notes:
The above will require your app to have permission to write to external storage: (you could alter it to use internal storage if you don't want that). In AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You will also need to give your RelativeLayout an id (I used "@+id/cardlayout" in the above example)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.