簡體   English   中英

如何在android中共享圖像和textview

[英]how to share image and textview over it in android

我的代碼僅共享圖像而沒有textView,該用戶將其放在圖像上。 我想通過任何應用程序共享邀請卡(圖像和文本在一起)。 不只是圖像

所以我的問題是如何在圖像上共享名稱為g7和textview且具有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)分享渲染的位圖

要渲染卡 ,您可以使用類似以下的功能。 它將View呈現到位圖上(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" />

您還需要給您的RelativeLayout一個ID(在上面的示例中,我使用了“ @ + id / cardlayout”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM